dougu5847 2013-07-15 10:33
浏览 86
已采纳

静态方法与非静态方法

Below are the examples of php class code that is static method and non static method.

Example 1:

class A{
    //None Static method
    function foo(){
        if (isset($this)) {
            echo '$this is defined (';
            echo get_class($this);
            echo ")<br>";
        } else {
            echo "\$this is not defined.<br>";
        }
    }
 }

 $a = new A();
 $a->foo();
 A::foo();

 //result
 $this is defined (A)
 $this is not defined.

Example 2:

class A{
    //Static Method
    static function foo(){
        if (isset($this)) {
            echo '$this is defined (';
            echo get_class($this);
            echo ")<br>
";
        } else {
            echo "\$this is not defined.<br>
";
        }
    }
 }

 $a = new A();
 $a->foo();
 A::foo();

 //result
 $this is not defined.
 $this is not defined.

I am trying to figure out what is the difference between these two Classes.

As we can see on the result of the none static method, the "$this" was defined.

But on the other hand the result on the static method was not defined even they were both instantiated.

I am wondering why they have different result since they were both instantiated?

Could you please enlighten me on what is happening on these codes.

  • 写回答

2条回答 默认 最新

  • douzhaiya3968 2013-07-15 11:00
    关注

    Before we go any further: Please, get into the habbit of always specifying the visibility/accessibility of your object's properties and methods. Instead of writing

    function foo()
    {//php 4 style method
    }
    

    Write:

    public function foo()
    {
        //this'll be public
    }
    protected function bar()
    {
        //protected, if this class is extended, I'm free to use this method
    }
    private function foobar()
    {
        //only for inner workings of this object
    }
    

    first off, your first example A::foo will trigger a notice (calling a non-static method statically always does this).
    Secondly, in the second example, when calling A::foo(), PHP doesn't create an on-the-fly instance, nor does it call the method in the context of an instance when you called $a->foo() (which will also issue a notice BTW). Statics are, essentially, global functions because, internally, PHP objects are nothing more than a C struct, and methods are just functions that have a pointer to that struct. At least, that's the gist of it, more details here

    The main difference (and if used properly benefit) of a static property or method is, that they're shared over all instances and accessible globaly:

    class Foo
    {
        private static $bar = null;
        public function __construct($val = 1)
        {
            self::$bar = $val;
        }
        public function getBar()
        {
            return self::$bar;
        }
    }
    $foo = new Foo(123);
    $foo->getBar();//returns 123
    $bar = new Foo('new value for static');
    $foo->getBar();//returns 'new value for static'
    

    As you can see, the static property $bar can't be set on each instance, if its value is changed, the change applies for all instances.
    If $bar were public, you wouldn't even need an instance to change the property everywhere:

    class Bar
    {
        public $nonStatic = null;
        public static $bar = null;
        public function __construct($val = 1)
        {
            $this->nonStatic = $val;
        }
    }
    $foo = new Bar(123);
    $bar = new Bar('foo');
    echo $foo->nonStatic, ' != ', $bar->nonStatic;//echoes "123 != foo"
    Bar::$bar = 'And the static?';
    echo $foo::$bar,' === ', $bar::$bar;// echoes 'And the static? === And the static?'
    

    Look into the factory pattern, and (purely informative) also peek at the Singleton pattern. As far as the Singleton pattern goes: Also google why not to use it. IoC, DI, SOLID are acronyms you'll soon encounter. Read about what they mean and figure out why they're (each in their own way) prime reasons to not go for Singletons

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

报告相同问题?

悬赏问题

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