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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配