Home  >  Article  >  Backend Development  >  Third party liability insurance compensation scope PHP scope analysis operator:: meaning analysis explanation

Third party liability insurance compensation scope PHP scope analysis operator:: meaning analysis explanation

WBOY
WBOYOriginal
2016-07-29 08:45:561535browse

I saw several symbols related to PHP today. One is @, which is added in front of a variable to suppress the PHP interpreter from reporting errors, which means that even if an error occurs, it will not be displayed.
There is also a more important symbol 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 in this case.

Copy code The code is 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, this will output
// I am the original function A::example().

A::example();
// Create a B Object of class
$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 here, so it cannot This calls example() using $a->example() or similar method. Instead we call example() as a class function, that is, as a function of the class itself, rather than any object of this class.
There are class functions here, but no class variables. In fact, there is no object at all when the function is called. Thus a class's functions may not use any objects (but may use local or global variables), and may not use the $this variable at all.
In the above example, class B redefines the function example(). The originally defined function example() in class A will be masked and will no longer take effect unless the :: operator is used to access the example() function in class A. For example: A::example() (actually, it should be written as parent::example(), which will be introduced in the next chapter).
For that matter, for the current object, it may have object variables. So you can use $this and object variables inside object functions.

The above introduces the scope of third-party liability insurance compensation and the analysis and explanation of the meaning of PHP's scope analysis operator::, including the content of the scope of compensation for third-party liability insurance. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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