Static keywords in PHP and their differences with self keywords

Source: Internet
Author: User
This article describes the differences between the static keyword and the self keyword in PHP. This article describes the definition of the static keyword, LateStaticBindings, and the differences between the static keyword and the self keyword, for more information, see Overview

I am studying the design pattern. I have read this article about Singleton pattern again. I found that the static keyword is not very reliable and I will review it.

Static keywords

The following describes the static keywords in the PHP manual:

The code is as follows:


Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can ).


Generally, after declaring the attributes and methods of a class as static, you can directly access static attributes and methods without instantiating objects.

PHP static members and methods have the following features:

1. static members cannot be accessed through class instances, but static methods are acceptable.
2. static members cannot be accessed through the-> operator.
3. the $ this keyword cannot appear in the scope of the static method, that is, the common member variables cannot be accessed in the static method.
4. static members and methods can be directly accessed by class names without instantiating objects.

Late binding (Late Static Bindings)

The following content is taken from the PHP manual:

The code is as follows:


Since PHP 5.3.0, PHP has added a function called static binding later, which is used to reference static calling classes within the inheritance scope.
To be precise, static binding is used to store the class name of the previous non-forward call. When a static method is called, the class name is the one explicitly specified (usually on the left side of the: operator). when a non-static method is called, that is, the class to which the object belongs. The so-called "forward call" refers to the static call through the following methods: self ::, parent ::, static :: and forward_static_call (). Use the get_called_class () function to obtain the class name of the called method. static: indicates its range.


For more information about this feature, see the examples in the following Manual.

Self vs static

Use a demo to explain the difference between self and static.
Self example:

The code is as follows:


<? Php
Class Vehicle {
Protected static $ name = 'This is a vehicle ';
Public static function what_vehicle (){
Echo get_called_class (). "\ n ";
Echo self: $ name;
}
}
Class Sedan extends Vehicle {
Protected static $ name = 'This is a Sedan ';
}
Sedan: what_vehicle ();


Program output:

The code is as follows:


Sedan
This is a Vehicle


Static example:

The code is as follows:


<? Php
Class Vehicle {
Protected static $ name = 'This is a vehicle ';
Public static function what_vehicle (){
Echo get_called_class (). "\ n ";
Echo static: $ name;
}
}
Class Sedan extends Vehicle {
Protected static $ name = 'This is a Sedan ';
}
Sedan: what_vehicle ();

Program output:

The code is as follows:


Sedan
This is a Sedan

Summary

I have read the previous article, but I haven't updated my blog for more than a month. I am busy with it. I still need to stick to it later. This article does not seem to have been written.

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.