The difference between this self static in PHP ____php

Source: Internet
Author: User
Tags ming
reproduced from: https://blog.csdn.net/mrzhouxiaofei/article/details/78648079 introduced

Recently in the course of doing software engineering, encountered a problem, to grilled Laravel source code, and search, found that the self static understanding is not deep, before there is a problem, so record down, to avoid the same mistakes again. Body This

This is better understood to point to the current object, which is used to access the non-static and Non-static methods of the current object, and it is related to the object;

<?php
class Person {public
    $name;

    Public Function GetName () {
        echo $this->name;
    }
}

$p = new Person ();
$p 2 = new Person ();
$p->name = "Little Red";
$p 2->name = "Xiaoming";
$p->getname ();  Small red
$p 2->getname ();  Xiao ming

In the example above, two objects were new and the Name property of the object was set, and this was used in GetName () to access the Name property of the current object, so the value of name was output respectively. So, this is pointing to the current object, not to other objects or classes. Self

Unlike self and this, it points to the class itself and does not point to any instantiated objects, which are generally used to access static variables and static methods in the class;

<?php
class Person {public
    static $name = "Little Red";

    public static function GetName () {
        echo self:: $name;
    }
}

$p = new Person ();
$p 2 = new Person ();
$p:: GetName ();  Small red
$p 2::getname ();  Small red
$p:: $name = "Xiaoming";
$p:: GetName ();  Xiaoming
$p 2::getname ();  Xiao ming

In the example above, two objects were new and the Name property of one of the objects was modified, and the value of the Name property of the other was changed, so that self is pointing to the current class, not the object, and all objects sharing a value. Static

Static and self are all pointing to a class, are generally used to access static variables and static methods in a class, but there are some different, specifically: which class self is written in, which is actually called, and Static is written in the parent class, and then the static is used by subclasses, This static refers to this subclass, which is officially called late static binding.

<?php
class A {public
    function say () {
        echo "Hello";
    }

    Public Function sayself () {
        self::say ();
    }

    Public Function saystatic () {
        static::say ();
    }
}

Class B extends A {public
    function say () {
        echo ' world ';
    }
}

$b = new B ();
$b->say ();  World
$b->sayself ();  Hello
$b->saystatic ();  World

As you can see in the example above, self is written in class A, the call points to a class, Static is also written in Class A, but the class Class B object of Class A is called, but it points to B, and when used, static determines which class to point to, and this is the late static binding.

Late static binding summary

This refers to the current object, which is used to access non-static and non-static methods of the current object;
Self points to a class that is typically used to access static variables and static methods of the current class, which are already determined before running;
Static points to classes that are generally used to access static and static methods of the current class, but are not limited to static calls, and the runtime determines which class to point to.

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.