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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器