The difference between “public” and “public static”?

后端 未结 5 904
小鲜肉
小鲜肉 2021-01-30 06:58

What does static mean?

I know public means that it can be accessed from outside the class, and private only from inside the class…

5条回答
  •  礼貌的吻别
    2021-01-30 07:20

    An example: when use the static keyword, we cannot use $this

    class Foo {
        private $foo='private';
    
        private function priv_func() {
            echo 'priv_method';
        }
    
        public static function get() {
            echo $this->foo;
            $this->priv_func();
        }
    }
    
    $obj = new Foo();
    $obj->get();
    

    Fatal error: Using $this when not in object context in (…)

提交回复
热议问题