Description of the self keyword in php programming. LieHuo. Net (LieHuo. Net) programming documentation self generally points to the static methods and constants of the current class and uses self: To add method names and constant names for reference. $ This indicates the instance of the current class.
Bkjia. Com programming documentSelf generally points to the static methods and constants of the current class and is referenced using self: method name and constant name. $ This indicates the instance object pointing to the current class. it is referenced using $ this-> adding method name and instance variable. In some callback methods, you can use the string 'self 'to point to the current class, rather than directly using self, such as call_user_func ('self', $ method.
In addition, self always references the method and constant of the current class. the subclass calls the static method of the parent class, and the self in the parent class method still points to the parent class itself, if the method of the subclass with the same name overwrites the parent class method, you can use parent: to reference the parent class method.
Reference content is as follows: Interface AppConstants { Const FOOBAR = 'Hello, World .'; }
Class Example implements AppConstants { Public function test (){ Echo self: FOOBAR; } }
$ Obj = new Example (); $ Obj-> test (); // outputs "Hello, world ."
Class MyClass { Const NAME = 'foo ';
Protected function myFunc (){ Echo "MyClass: myFunc () \ n "; } Static public function display (){ Echo self: NAME; } Static public function getInstance (){ $ Instance = new self; Return $ instance; } }
Class ChildClass extends MyClass { Const NAME = 'child ';
// Override parent's definition Public function myFunc (){ // But still call the parent function Parent: myFunc (); Echo "ChildClass: myFunc () \ n "; } }
$ Class = new ChildClass (); $ Class-> myFunc ();
Echo ('class constant :'); ChildClass: display (); Echo ('object class :'); Echo (get_class (ChildClass: getInstance ())); ?> |
The LieHuo. Net programming document self generally points to the static methods and constants of the current class and is referenced using the self: method name and constant name method. $ This indicates the instance of the current class...