Home  >  Article  >  Backend Development  >  Detailed explanation of the use of PHP class constants_PHP tutorial

Detailed explanation of the use of PHP class constants_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:08:031041browse

Note: Unlike other object-oriented programming languages, in PHP, a class cannot use the final modifier for a property variable.
If you want to declare a property as a constant, you can use the const keyword and there is no need to prefix the variable name with a dollar sign or use the access permission modifier. Constant means that although the variable can be accessed, the value of the variable cannot be modified. For example, the following code declares the constant attribute con_var:

Copy the code The code is as follows:

class Foo{
const con_var="The value of a constant attribute cannot be modified
";
public function method_a(){
echo (self::con_var);
}
}
echo(Foo::con_var);
$myFoo=new Foo();
echo ($myFoo->method_a());
?>

Constant attributes cannot be accessed using objects, but can only be accessed using classes. Within the class body, you can use "self::constant name", and outside the class body, you can use "classname::constant". name".

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327490.htmlTechArticleNote: Unlike other object-oriented programming languages, in PHP, a class cannot use final for a certain attribute variable. modifier. If you want to declare a property as a constant, you can use the const keyword...
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