The content shared with you in this article is about the usage examples of the keyword var in PHP. The content is of great reference value. I hope it can help friends in need.
1 class Test{ 2 var $a=123;//访问控制,这里的var不用就不正常,当然你可以用public protected等关键词代替,来声明成员变量的属性 3 4 } 5 $obj=new Test(); 6 echo $obj->a; 7 //打印结果:123
If you replace var with public, it will work the same.
But at this time, if you remove var in the class and there is no access modifier, it will prompt a syntax error.
In fact, after testing, I believe that var is an alias for public and is used to define public properties in a class. However, due to historical issues, var is no longer used now. Later, I checked the php official website and it turned out to be true.
php official explanation:
Class attributes must be defined as one of public, protected, and private. If defined with var, it is considered public.
Note: For compatibility reasons, the method of defining variables using the var keyword in PHP 4 is still valid in PHP 5 (just as an alias for the public keyword). In versions prior to PHP 5.1.3, this syntax will generate an E_STRICT warning
Related recommendations:
How are PHP variables defined and how does PHP work?
The above is the detailed content of Usage examples of keyword var in PHP. For more information, please follow other related articles on the PHP Chinese website!