PHP Object-oriented Programming the difference between self and static MATLAB object-oriented Programming C # Object-oriented programming idea object-oriented programming language

Source: Internet
Author: User
In the object-oriented programming of PHP, you will always encounter

Class test{public static function test () {  self::func ();  Static::func (); } public static function Func () {}}

But do you know the difference between self and static?

In fact, the difference is very simple, just need to write a few demo can understand:

Demo for self:

Class car{public static function model () {  self::getmodel ();} protected static function Getmodel () {  echo ' this is A car model "; }}

Car::model ();

Class Taxi extends car{protected static function Getmodel () {  echo "This is a Taxi model";}}

Taxi::model ();
Get output

This was a car modelthis is a car model

It can be found that self in a subclass still calls the parent class's method

Demo for static

Class car{public static function model () {  static::getmodel ();} protected static function Getmodel () {  echo ' this is a car model "; }}car::model (); Class Taxi extends car{protected static function Getmodel () {  echo "This is a Taxi model";}} Taxi::model ();

Get output

This was a car modelthis is a Taxi model

As you can see, in the call to static, the subclass even calls the method of the parent class, but the method called in the parent class method is also a method of the subclass (good Raozui: )

Before the PHP5.3 version, static and self still have a little difference, specifically what, after all, is the 7 version of the world. Don't get to know it.

The summary is that self can only refer to methods in the current class, while the static keyword allows a function to dynamically bind methods in a class at run time.

The above describes the PHP object-oriented programming self and static differences, including object-oriented programming, static content, I hope the PHP tutorial interested in a friend helpful.

  • 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.