The difference between PHP static and self

Source: Internet
Author: User

PHP is the best language in the world

PHP from the process to the now mature object-oriented system, in PHP object-oriented, static variable call We can use these two self::method and Static::method, but many children's shoes confused, do not understand Self::method and static:: What is the difference between method, here are two examples to see exactly:

Example 1:

1 classCar {2      Public Static functionmodel ()3     {4Self::Getmodel ();5     }6     protected Static functionGetmodel ()7     {8         Echo"This is a car model";9     }Ten } OneCar::model ();//This is a car model A Echo' <br/> '; - ClassTaxiextendsCar { -     protected Static functionGetmodel () the     { -         Echo"This is a Taxi model"; -     } - } +Taxi::model ();//This is a car model

Summary: Self::getmodel () calls Method Getmodel (), the method of the subclass Getmodel () does not actually mean that the parent class is overloaded with methods Getmodel ().

Example 2:

1 classCar {2      Public Static functionmodel ()3     {4         Static::Getmodel ();5     }6     protected Static functionGetmodel ()7     {8         Echo"This is a car model";9     }Ten } OneCar::model ();//This is a car model A Echo' <br/> '; - ClassTaxiextendsCar { -     protected Static functionGetmodel () the     { -         Echo"This is a Taxi model"; -     } - } +Taxi::model ();//This is a Taxi model

Summary: Self::getmodel () calls Method Getmodel (), the method of the subclass Getmodel () overloads the parent class's Method Getmodel ().

Children's shoes, do you understand?

If you have doubts, please comment

The difference between PHP static and self

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.