The static keyword in PHP and the difference with the self keyword, staticself

Source: Internet
Author: User

The static keyword in PHP and the difference with the self keyword, staticself

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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe Code is as follows:
Sedan
This is a Vehicle

Static example:
Copy codeThe 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:
Copy codeThe 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.