Instructions on self keywords in php programming _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.