Usage examples of keyword var in PHP

不言
Release: 2023-04-03 11:28:01
Original
1895 people have browsed it

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
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!