The difference between the double colon range parsing operator and the arrow -> operator in PHP

黄舟
Release: 2023-03-12 11:50:02
Original
3657 people have browsed it

Double colons are generally used in calls to static methods and static variables. At this time, the class does not need to be instantiated. Instead, when using the arrow operator - >, the class must be instantiated (or it can be called inside the class.)

Double colon (::) is positional access symbol. It doesn't just mean static methods and variables. It means that the thing on the left side of the colon calls the thing on the right side of the colon. Such a meaning. so. . $self cannot be written on the left. Because $self is parsed first, unless the variable $self is defined previously, it points to itself.
In the latest version of php. The double colon is used exactly to follow this function. . Can be replaced - > Call object variable. Because it is a position indicator. .

is to distinguish the methods and attributes of the object, and to access the static methods and static variables of the class. The static methods and static variables of the class are public to the class and can be accessed without instantiation, while the methods of the object and properties are specific to each object and therefore must be instantiated first. The same goes for other languages ​​such as C++, JAVA, etc. (Of course, the access symbols they provide may be different)

- > is to access the methods and properties of the object, :: is to access the static methods and static variables of the class

->Object access and ::location access

Everyone has said it before about the theoretical part, and it is indeed correct. But php is a little different.
In the default configuration, php does not have strict requirements:: The static method must be called
You can know by testing the following code

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();
    }
}


作者:楚天乐
链接:https://www.zhihu.com/question/19782824/answer/35943595
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Copy after login

The above is the detailed content of The difference between the double colon range parsing operator and the arrow -> operator in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template