Home > Backend Development > PHP Tutorial > Analysis and explanation of the use of PHP's range parsing operator (::)

Analysis and explanation of the use of PHP's range parsing operator (::)

黄舟
Release: 2023-03-12 11:32:01
Original
1120 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.

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 scope resolution Operator (::)
Access the function or base in the class without declaring any instance Functions and variables in classes are very useful. The :: operator is used in this case.

<?php 
class A { 
function example() { 
echo "I am the original function A::example().<br />\n"; 
} 
} 
class B 
extends
 A { 
function example() { 
echo "I am the redefined function B::example().<br />\n"; 
A::example(); 
} 
} 
// A 类没有
对象
,这将输出 
// I am the original function A::example().<br /> 
A::example(); 
// 建立一个 B 类的对象 
$b = new B; 
// 这将输出 
// I am the redefined function B::example().<br /> 
// I am the original function A::example().<br /> 
$b->example(); 
?>
Copy after login


The above example calls the function example() of class A, but there is no object of class A here, so it cannot be called with $a->example() or similar methods. example(). 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).
As such, for the current object, it may have object variables. So you can use $this and object variables inside object functions.

The above is the detailed content of Analysis and explanation of the use of PHP's range parsing operator (::). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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