PHP Scope resolution operators (::) meaning analysis of the explanation

Source: Internet
Author: User
Today I see a few symbols about PHP. One is the @, which is added to the front of a variable to suppress the PHP interpreter's error, which means it doesn't show up even if it's wrong.
There's a more important symbol. PHP Range Resolution operator (::)
It is useful to access functions in a class or functions and variables in a base class without declaring any instances. And:: The operator is used in this case.
Copy CodeThe code is as follows:
<?php
Class A {
function Example () {
echo "I am" original function a::example (). <br/>\n ";
}
}
Class B extends A {
function Example () {
echo "I am" redefined function b::example (). <br/>\n ";
A::example ();
}
}
Class A has no objects, which will output
I am the original function a::example (). <br/>
A::example ();
Create an object of class B
$b = new B;
This will output
I am the redefined function b::example (). <br/>
I am the original function a::example (). <br/>
$b->example ();
?>

The example above calls the function example () of Class A, but there is no object of Class A, so it is not possible to invoke example () in $a->example () or similar methods. Instead we call example () as a class function, that is, as a function of a class itself, rather than any object of this class.
Here are the class functions, but there are no variables for the class. In fact, there is no object at all when calling a function. Thus a function of a class may not use any object (but it can use local or global variables), and $this variables may not be used at all.
In the example above, class B redefined the function example (). The original defined function example () in Class A is masked and no longer takes effect unless the example () function in Class A is accessed by using the: operator. such as: A::example () (in fact, should be written as parent::example (), the next chapter describes the content).
In this context, for the current object, it may have object variables. You can therefore use $this and object variables within an object function.

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.