doulu0266 2014-11-04 23:23
浏览 195
已采纳

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

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

Example 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();

Or Example 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();

Thanks in advance

  • 写回答

2条回答 默认 最新

  • doubei8168 2014-11-04 23:38
    关注

    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
    

    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.

    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']);
    

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog