在一个类中混合使用静态和非静态方法是否可以接受?

在一个类中混合使用静态和非静态方法是否可以接受?

问题描述:

我有一个相对简单的问题,尽管在Google上有很多关于它的帖子,但我找不到一个简单地回答该问题的问题.

I have a relatively simple question, and although there are many posts about it on Google, I cannot find a single one that simply answers the question.

因此,一个简短的问题是在一个类中混合使用静态和非静态方法是否可以接受?".我猜我真的是在问坚持一种方法是一种好的做法",还是同时使用这两种方法时要考虑的事情".

So the short question is "Is it acceptable to mix static and non static methods in one class?". I guess I am really asking "is it good practice to stick with one type of method", or "are there things to consider when using both".

例如,如果我要建立一个课程来应对冰箱里的食物,那么以下哪种方法(或其他什么方法)将是最好的方法

For example, If I was building a class to cope with food in my fridge, which of the following (or what else) would be the best approach

示例1:

Class Food
{
    public function __construct( $itemName, $itemDescription )
    {
        .... some code for a new item of food ....
    }

    public static function getAllFood()
    {
        .... some code to get all the items in my fridge ....
    }
}

$food = new Food( "apple", "Nice juicy Pink Lady apple" );
Food::getAllFood();

或示例2:

Class Food
{
    public function __construct( $itemName, $itemDescription )
    {
        .... some code for a new item of food ....
    }

    public function getAllFood()
    {
        .... some code to get all the items in my fridge ....
    }
}

$food = new Food( "apple", "Nice juicy Pink Lady apple" );
$food->getAllFood();

预先感谢

在这种情况下,您必须使用示例2,因为您在示例1中尝试执行的操作将无效:

In this case you have to go with example 2, because what you're trying to do in example 1 will not work:

$food = new Food( "apple", "Nice juicy Pink Lady apple" );
Food::getAllFood(); //static

除非课程中有一些硬编码,否则不会返回任何食物.您在构造函数中输入的内容将被放入 $ food 中的实例中.但是您要调用类本身 Food :: getAllFood()进行检索.那没有道理.

There will not be any food returned unless there's some hardcoded in the class. What you put in with the constructor, you put into the instance in $food. But you're calling the class itself Food::getAllFood() to retrieve. That doesn't make sense.

在某些情况下,在类中包含静态方法是否有意义?是的.就像我正在制作一个数据库连接类以保留一个连接一样,但是我也想公开一些与特定DBMS相关的日期格式方法,但实际上并不需要连接到数据库的连接处于活动状态.接触数据库的任何东西都必须是非静态的,但是我已经提取的数据上的某些转换方法可能是静态的:

Are there cases where it would make sense to include a static method in a class? Yes. Like if I was making a db connection class that would hold a connection, but I also wanted to expose some date format methods that are related to the particular DBMS but don't actually require the connection to the db to be active. Anything touching the db would have to be non-static, but some transformation methods on data I've already pulled out could be static:

$db = new JDE_DBClass($connectionString);
$rows = $db->doSelectQuery("select * from whatever");
$date = JDE_DBClass::convertJDE_Date_to_PHP_Date($rows[0]['dateField']);

在这种情况下,您可能需要执行此操作以允许进行转换而不必实例化db对象,因为在确定是否需要进行转换之前,可能需要在JDE所谓的Julian格式和常规日期之间来回转换.连接到数据库:

In this case you might want to do this to allow conversion without having to instantiate the db object, because perhaps you might need to convert back and forth between JDE's so-called Julian format and regular dates before even determining whether you need to connect to the db:

$date = JDE_DBClass::convertJDE_Date_to_PHP_Date('114309');