Compensation scope of third party liability insurance PHP Scope resolution operator:: Meaning analysis description

Source: Internet
Author: User
Tags php operator
See a few symbols about PHP today. One is @, this is added to the front of a variable, to suppress the PHP interpreter error, that is, even if the error is not displayed.
There is also a more important symbol for PHP's 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 for this case.

Copy the Code code as follows:


Class A {
function Example () {
echo "I am the original function a::example ().
\ n ";
}
}
Class B extends A {
function Example () {
echo "I am the redefined function b::example ().
\ n ";
A::example ();
}
}
Class A has no objects, which will output
I am the original function a::example ().

A::example ();
Create an object of class B
$b = new B;
This will output
I am the redefined function b::example ().

I am the original function a::example ().

$b->example ();
?>


The above example calls the function example () of Class A, but there is no object of Class A, so you cannot call example () with the $a->example () or a similar method. Instead, we call example () as a class function, that is, as a function of a class itself, instead of any object of this class.
There are class functions, but there are no classes of variables. In fact, there is no object at all when the function is called. Thus a function of a class can use no object (but local or global variables can be used), and you can not use $this variable 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 valid unless you use the:: operator to access the example () function in Class A. such as: A::example () (In fact, it should be written as parent::example (), the next chapter describes the content).
For this purpose, it may have object variables for the current object. You can therefore use $this and object variables inside an object function.

The above describes the third party liability insurance scope of the scope of the interpretation of PHP Operator:: The meaning of the analysis, including the third party liability insurance compensation scope of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.