difference between public and public static?

2019-03-08 02:12发布

what does static mean?

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

4条回答
狗以群分
2楼-- · 2019-03-08 02:26

Some example ... When use static keyword then we cannot use $this..

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

$obj->ger();

Fatal error: Using $this when not in object context in

查看更多
The star\"
3楼-- · 2019-03-08 02:27

from http://php.net/manual/en/language.oop5.static.php

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

查看更多
姐就是有狂的资本
4楼-- · 2019-03-08 02:36

Static means that it can be accessed without instantiating a class. This is good for constants.

Static methods need to have no effect on the state of the object. They can have local variables in addition to the parameters.

查看更多
Fickle 薄情
5楼-- · 2019-03-08 02:37

public: Public declared items can be accessed everywhere.

protected: Protected limits access to inherited and parent classes (and to the class that defines the item).

private: Private limits visibility only to the class that defines the item.

static: A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

final: Final keyword prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

transient: A transient variable is a variable that may not be serialized.

volatile: a variable that might be concurrently modified by multiple threads should be declared volatile. Variables declared to be volatile will not be optimized by the compiler because their value can change at any time.

查看更多
登录 后发表回答