douning5041 2013-10-07 12:57
浏览 14
已采纳

是否有必要在此类中使用静态方法和属性? [关闭]

I'm studying OOP in PHP. And I'm confused about using static methods and properties.

In this example I will use a little cache example on server side. But I'm not sure to use static method and property.

class Villa extends CI_Model
{
  // Cache
  protected $cacheOwner;

  ...   

  public function owner()
  {
   if ( ! empty($this->cacheOwner))
        return $this->cacheOwner;

I think it will work but to use "static" is better? Or I must use "static" only when inherits a property or method from a parent class?

  • 写回答

2条回答 默认 最新

  • dongwo5449 2013-10-07 16:47
    关注

    I'm studying OOP in PHP. And I'm confused about using static methods and properties.

    In this example I will use a little cache example on server side. But I'm not sure to use static method and property

    So, how to create and use static method/properties :

    class Villa extends CI_Model
    {
        protected static $cacheOwner = null;
    
        public static function owner()
        {
            if ( ! is_null(static::$cacheOwner) ) {
                return static::$cacheOwner;
            }
        }
    }
    

    Use it as

    Villa::owner();
    

    I think it will work but to use "static" is better? Or I must use "static" only when inherits a property or method from a parent class?

    No, it's not right at all because static considered harmful and anti-pattern. In the term object oriented programming where Objects encapsulate state and provide methods to transform this state but when you use static properties and methods it violates the principle because it (static) preserves the same state on all instantiated objects and hence it's a class oriented programming, not OOP. In OOP classes are just blueprints/data type and objects are the things that comes in to play. Also static methods/classes doesn't facilitate unit testing because of hard dependency and also hard to extend. In some cases like singleton design pattern, static class methods/properties are probably useful but it's (singleton) itself considered a bad design pattern and out of OOP. Check SOLID and other links provided in (See also) section.

    So, try to avoid static and finally, none can tell you what actually you need in your class but you, because you know what you are going to do and how is your class related with your application and if you need to maintain the same state of a property/data all the way to your application's lifetime then you may use it but not sure. Also, check this search result and read some good answers, you'll learn a lot from here, at least I did a lot and i found this article with some examples, could be helpful too.

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