The difference between the double colon range resolution operator in PHP and the arrow-to operator

Source: Internet
Author: User
A double colon is usually called by a static method, a static variable, and the class does not need to be instantiated at this time. Instead of using the arrow operator->, you must instantiate the class (or call it inside the class). )

A double colon (::) is a positional access symbol. Not just static methods and variables. Instead, it means calling the colon to the left of this lump called the right side of the colon. Such a meaning. So.. The left cannot write $self. Because $self is parsed first, unless $self is defined earlier, the variable is pointing to itself.
In the latest version of PHP. A double colon is a way to follow this function completely. Can replace-> Invoke object variable. Because it's a positional indicator.

In order to distinguish between the methods and properties of an object, and to access static and static variables of a class, static and static variables of the class are common to the class, do not need to be instantiated and can be accessed, and the methods and properties of the objects are unique to each object and must be instantiated first. Other languages, such as C++,java, are the same (although they may not provide the same access symbols)

-> is the method and property that accesses the object:: Is the static method and static variable that accesses the class

Object access and:: Location access

The theory part, as everyone has said before, is indeed true. But PHP is a little different.
In the default configuration, PHP is not strictly required:: The static method must be called
Test the following code to know

Class t{public    $v = 1;    Public Function GetVar () {        return 1;//$this->v;    }} Class Xxxxx{public function Actionindex ()    {$x = T::getvar ();        $t = new T ();        $x = $t->getvar ();    }} Author: Chu Tianle Link: https://www.zhihu.com/question/19782824/answer/35943595 Source: The copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

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.