Home > Backend Development > PHP Tutorial > Explanation of meaning analysis of PHP's scope parsing operator (::)_PHP Tutorial

Explanation of meaning analysis of PHP's scope parsing operator (::)_PHP Tutorial

WBOY
Release: 2016-07-21 15:26:46
Original
1064 people have browsed it

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();
}
}
// A The class has no objects, this will output
// I am the original function A::example().

A::example();
// Create a class B Object
$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 a function of class A example(), but there is no object of class A here, so example() cannot be called using $a->example() or similar methods. 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 function 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 function example() originally defined 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 is 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323907.htmlTechArticleI 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. One more...
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