Double Colon in PHP:: Introduction to the meaning of the scope resolution operator

Source: Internet
Author: User
Tags access properties
PHP code Common "::" Operators, this is the scope of the qualified operator, which is used to pin class in the non-scoped level, the left is the scope, the right is the access scope of the members.


Scoped operator (also known as: Range resolution operator) or, more simply, a pair of colons,
Can be used to access static members, methods, and constants, and can also be used to override members and methods in a class.

The scope defined in PHP has self and parent, and the static scope is provided in PHP6.

Self: Represents the scope of the current class, cannot use self in code outside the class, and when using self in an extension class, it calls a method other than the parent class, but instead extends the overloaded method of the class.

Parent: Represents the scope of the current class parent class.

<?php

Class forasp{

Static $url="http://www.4u4v.cn";

Static $webname = "Web production learning php double colon";

Public function writeurl(){

Echo self::$url; //call your own content

}

Public function writewebname(){

Echo "test subclass calls parent class content";

}

}

Class cn extends forasp{

Function father(){

Parent::wirtewebname();

}

}

$a = new forasp();//instantiating the parent class

$a->writeurl();//call its own content

$b = new cn();

$b->writewebname();//call parent content

?>

When you call a static method, you can use:: Call a static method or property in a class.

Usage:

The double-colon operator, the scope-scoped operator, scope Resolution operator can access properties and methods overridden in static, const, and class.

So, what is the difference between the application and the arrow-----operator?

When you access these static members, methods, and constants outside of the class, you must use the name of the class.
The double-colon range-resolution operator is typically used in static methods, static variable calls, where the class does not need to be instantiated.
With the arrow operator, you must instantiate the class (or you can call it inside the class).

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.