Home > Article > Backend Development > What\'s the Role and Evolution of the `var` Keyword in PHP?
Exploring the Usage of 'var' Keyword in PHP
The 'var' keyword in PHP has served as a method of declaring class member variables, particularly during the PHP4 era. However, its relevance has diminished in PHP5 and beyond. Nevertheless, understanding its behavior and changes is beneficial for both developers familiar with its legacy and those seeking to enhance their understanding of PHP's evolution.
Declaration in PHP4
Prior to PHP5, 'var' was employed to define class member variables. It resembled the syntax used for declaring PHP variables with the $ sign:
class foo { var $x = 'y'; }
PHP5 Modifications
With the advent of PHP5, 'var' became obsolete. PHP5 introduced the 'public' keyword to explicitly declare class member variables with public visibility. While 'var' still functioned, it emitted an E_STRICT warning in PHP versions 5.0.0 to 5.1.2 due to its deprecated status.
Un-Deprecation in PHP5.3
From PHP5.3 onward, 'var' underwent a reversal in its deprecation status. It effectively became synonymous with 'public' and could be used interchangeably for declaring class member variables.
Example Usage
Let's consider a sample code illustrating the use of 'var' and 'public':
class foo { var $x = 'y'; //or you can use public like... public $x = 'y'; //this is also a class member variables. function bar() { } }
While 'var' is still recognized in PHP5 and beyond, it is recommended to use 'public' for better coding practices and to avoid any potential legacy issues.
The above is the detailed content of What\'s the Role and Evolution of the `var` Keyword in PHP?. For more information, please follow other related articles on the PHP Chinese website!