Home >Backend Development >PHP Tutorial >What\'s the Deal with PHP\'s Obsolete `var` Keyword?

What\'s the Deal with PHP\'s Obsolete `var` Keyword?

Linda Hamilton
Linda HamiltonOriginal
2024-11-28 09:48:11417browse

What's the Deal with PHP's Obsolete `var` Keyword?

Deciphering PHP's 'var' Keyword: A Detailed Exploration

The 'var' keyword in PHP has been a persistent enigma, leaving many programmers scratching their heads. This article delves into its enigmatic nature, answering the following burning questions:

  1. What does the 'var' keyword signify in PHP?
    The 'var' keyword, introduced in PHP4, serves as a declaration of class member variables. It specifies the visibility and type of the variable within the class. However, the 'var' keyword is now obsolete and no longer required.
  2. Are there any PHP version-specific nuances with the 'var' keyword?
    Yes, there are differences between PHP versions. In PHP5, the 'var' keyword still functions but generates a PHP warning. Specifically, in PHP versions 5.0.0 to 5.1.2, it triggers an E_STRICT warning as it was deprecated. However, in PHP 5.3 and above, 'var' has been un-deprecated and is treated as a synonym for 'public.'

Usage Example

To illustrate its usage, consider the following PHP code:

class foo {
    var $x = 'y'; // or you can use public like...
    public $x = 'y'; //this is also a class member variables.
    function bar() {
    }
}

In this example, 'var' is used to declare the 'x' property as a member variable within the 'foo' class. Alternatively, 'public' can be used to achieve the same effect.

The above is the detailed content of What\'s the Deal with PHP\'s Obsolete `var` Keyword?. For more information, please follow other related articles on the PHP Chinese website!

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