Home >Backend Development >PHP Tutorial >What\'s the story behind PHP\'s `var` keyword and its evolution from PHP4 to PHP5?

What\'s the story behind PHP\'s `var` keyword and its evolution from PHP4 to PHP5?

Susan Sarandon
Susan SarandonOriginal
2024-11-29 10:32:09634browse

What's the story behind PHP's `var` keyword and its evolution from PHP4 to PHP5?

Exploring PHP Keyword 'var': Meaning and Evolution

Declaring class member variables in PHP4 necessitated the use of the 'var' keyword. In PHP5, however, the usage of 'var' has evolved.

PHP4: Declaration of Class Member Variables

In PHP4, 'var' held the primary responsibility for marking specific properties as class member variables. Essentially, it was the syntax that enabled data encapsulation within objects.

For instance, a class named 'foo' with a class member variable 'x' assigned as 'y' would be defined as follows:

class foo {
    var $x = 'y';
}

PHP5: Deprecation and Re-Introduction

PHP5 introduced a significant change in the handling of 'var'. The keyword was deprecated, issuing an E_STRICT warning when used. Yet, it remained functional in that it continued to work as it had in PHP4.

PHP 5.0.0 to 5.1.2: Deprecation

Within this PHP version range, 'var' was considered deprecated. Using it would trigger an E_STRICT warning, alerting developers to its future removal. However, it still retained its functionality for defining class member variables.

PHP 5.3: Un-Deprecation and Synonymity

With PHP 5.3, the 'var' keyword was un-deprecated. It became synonymous with the 'public' keyword, offering no additional functionality. In this sense, 'var' and 'public' essentially became interchangeable when defining class member variables.

Today, it is common practice to use 'public' instead of 'var'. Using 'var' is discouraged, even if still supported, as it may lead to confusion or outdated code maintenance issues.

The above is the detailed content of What\'s the story behind PHP\'s `var` keyword and its evolution from PHP4 to PHP5?. 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