Home > Backend Development > PHP Tutorial > Is PHP\'s `var` Keyword Still Relevant After the Introduction of `public`?

Is PHP\'s `var` Keyword Still Relevant After the Introduction of `public`?

Susan Sarandon
Release: 2024-11-27 09:08:11
Original
205 people have browsed it

Is PHP's `var` Keyword Still Relevant After the Introduction of `public`?

PHP's 'var' Keyword: A Relic from the Past

Initially introduced in PHP4, the 'var' keyword was utilized to declare class member variables. However, with the advent of PHP5, this syntax has become obsolete. PHP5 now utilizes the 'public' keyword to achieve the same functionality.

While 'var' remains functional in PHP5, it generates an E_STRICT warning for versions 5.0.0 through 5.1.2, indicating its impending deprecation. As of PHP 5.3, 'var' has been resurrected as a synonym for 'public,' effectively rendering it unnecessary.

To demonstrate its usage in PHP4:

class foo {
    var $x = 'y'; // Declares a class member variable
}
Copy after login

In PHP5 and beyond, this can be rewritten as:

class foo {
    public $x = 'y'; // Declares a class member variable
}
Copy after login

In conclusion, the 'var' keyword is a remnant of PHP4 that has been superseded by the 'public' keyword in PHP5. While it may still function in certain PHP5 versions, its use is strongly discouraged due to potential deprecation warnings and redundancy with the 'public' keyword.

The above is the detailed content of Is PHP\'s `var` Keyword Still Relevant After the Introduction of `public`?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template