Definition of the Static keyword in PHP, late binding, and the difference from the Self keyword

Source: Internet
Author: User
This article mainly introduces the definition of static keywords in PHP, the late binding and the difference with the Self keyword, interested in the friend's reference, I hope to help you.

Overview

is learning design patterns, there is an article about the singleton mode of the article, reread this article, found that the Static keyword mastery is not very strong, re-review.

Static keyword

The characteristics of static members and methods in PHP are as follows:

1. Static members cannot be accessed through instances of the class, but static methods can.
2. Static members cannot be accessed by operator.
3. In the scope of a static method, the $this keyword cannot appear, which means that normal member variables cannot be accessed in a static method.
4. Static members and methods can be accessed directly through the class name without having to instantiate the object.

Late binding (late Static Bindings)

Since PHP 5.3.0, PHP has added a feature called late static binding that references statically invoked classes within the scope of the inheritance.

To be exact, late static binding works by storing the class name on the previous "non-forwarding call" (Non-forwarding calls). When a static method call is made, the class name is the one that is explicitly specified (usually in the left part of the: operator), and the class to which the object belongs when a non-static method call is made. The so-called "forwarding calls" (forwarding call) refer to static calls made in the following ways: self::,parent::,static:: and Forward_static_call (). You can use the Get_called_class () function to get the class name of the method being called, Static:: It indicates its scope.


For an understanding of this feature, you can refer to the example in the following manual

Self vs Static

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

The code is as follows:

<?phpclass 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:

Sedanthis is a Vehicle

static Example:

The code is as follows:

<?phpclass 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:

Sedanthis is a Sedan

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

Ways to parse HTML files using the Snoopy class

Three common uses of PHP analog post requests

The definition and implementation of PHP for event mechanism

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.