dtja73027 2014-06-16 08:57
浏览 42
已采纳

在PHP中使用静态成员是否有最佳实践?

PHP allows use of static member functions and variables, since 5.3 including late static bindings:

class StaticClass {
    public static $staticVar;

    ...
}

$o = new StaticClass();

Currently, there are various options to access those static members:

$o->staticVar;  // as instance variable/ function
$o::staticVar;  // as class variable/ function

Other options exist for accessing members from inside the class:

self::$staticVar;   // explicitly showing static usage of variable/ function
static::$staticVar; // allowing late static binding

Restructuring some existing classes that make some use of static members I've asked myself if there are best practices for working with static members in PHP?

  • 写回答

2条回答 默认 最新

  • dourang8305 2014-06-16 09:08
    关注

    Well, obviously, they all do different things.

    $o->staticVar
    

    This is invalid, since you cannot/should not access static properties with the instance property syntax.

    StaticClass::$staticVar
    

    This very plainly accesses a specific static variable on a very specific class.

    $o::$staticVar
    

    This accesses the static variable on the class that $o is an instance of. It's mostly used as a shorthand for the previous method and is equivalent in all respects. Obviously though, which class is used exactly depends on what class $o is an instance of.

    self::$staticVar
    

    This can be used only inside a class, and will always refer to the class that it's written in. It's a good idea to use this inside a class instead of StaticClass::$staticVar if the class refers to itself, since you don't need to worry about anything if you change the class name later. E.g.:

    class Foo {
    
        protected static $bar = 42;
    
        public function baz() {
            self::$bar;  // good
            Foo::$bar    // the same, but should be avoided because it repeats the class name
        }
    
    }
    
    static::$staticVar
    

    This can also only be used inside a class and is basically the same as self above, but resolves with late static binding and may hence refer to a child class.

    What the "best practice" is is debatable. I'd say you should always be as specific as necessary, but no more. $o::$staticVar and static::$staticVar both allow the class to vary through child classes, while self::$staticVar and StaticClass::$staticVar do not. Following the open/closed principle, it's a good idea to use the former, more variable method to allow for extensions.

    Properties, both static and non-static, should also not be public to not break encapsulation.

    Also see How Not To Kill Your Testability Using Statics.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀