PHP Object-oriented Programming the difference between self and static, object-oriented programming self_php tutorial

Source: Internet
Author: User

PHP Object-oriented Programming the difference between self and static, object-oriented programming self


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.

http://www.bkjia.com/PHPjc/1123781.html www.bkjia.com true http://www.bkjia.com/PHPjc/1123781.html techarticle PHP Object-oriented Programming the difference between self and static, object-oriented programming self in the object-oriented programming of PHP, will always encounter class test{public static function test () {Self::func (); Static:: ...

  • Related Article

    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.