This is an old revision of the document!
PHP RFC: "var" Deprecation
- Date: 2016-02-20
- Author: Colin O'Dell colinodell@php.net
- Status: Draft
- Discussion: http://markmail.org/message/wn3ykdwgplfctho7
Introduction
var
is currently a simple alias for public
. Both keywords provide identical functionality. This RFC therefore proposes the deprecation of var
in favor of public
.
PHP 4 introduced the var
keyword as the only means to declare class properties.
PHP 5 added the public
, protected
, and private
keywords for declaring these properties with a certain visibility level. To quote the documentation:
“The PHP 4 method of declaring a variable with thevar
keyword [was] supported for compatibility reasons (as a synonym for thepublic
keyword”
(emphasis added)
This alias was left intact for PHP 7.
Proposal
This RFC proposes that var
be deprecated in PHP 7.1 and its functionality removed in PHP 8.0. var
would remain a reserved word in PHP 8.0 for potential future usage.
Pros
Removes Duplication
The primary reason for introducing this change is to eliminate duplicate functionality in the language. Both var
and public
work identically, so do we really need two different keywords which do exactly the same thing?
Low Usage
Of the top X,000 packages on Packagist, only X use var
, thus confirming that its usage is relatively low. This means fewer people will be impacted by this change.
Removes Legacy Functionality
var
is a legacy feature from PHP 4 which has been superseded by the visibility keywords. It was kept in PHP 5+ for backwards-compatibility. Removing it would help clean up the lesser-used legacy bits of the language.
There is precedent for deprecating (and eventually removing) other legacy features with modern equivalents in major releases:
Ample Warning
PHP 8.0 is at least a few years away. Deprecating this now (rather than later) would provide developers with sufficient notice that their code may need to be modified to work on 8.0.
Easy Upgrade Path
Because var
is currently an alias for public
, updating older code is as simple as replacing var
with public
everywhere it appears. A utility script has been provided with this RFC to aid with the transition.
Cons
It's Not Broken
There are no technical issues with the current implementation of var, and therefore no strong need to remove it.
Breaks Backwards Compatibility
PHP 4 classes will no longer function as-is in PHP 8.0. Furthermore, any classes written in PHP 5.x or 7.x may also break in 8.0 if they use var
. BC breaks should not be taken lightly.
No Functionality Benefit
This change does not introduce any new functionality or other benefit for developers. Those who are not using var
will see no impact. Those who do use var
will need to update their code.
Backward Incompatible Changes
PHP 7.1 - 7.x: usages of var
will raise a deprecation notice but otherwise work as-is.
PHP 8.0: Usages of var
will throw a parse error.
Upgrade Tool
An upgrade tool is provided which replaces all instances of var
with public
.
RFC Impact
To SAPIs
?
To Existing Extensions
?
To Opcache
?
Unaffected PHP Functionality
Classes which only use the PHP 5 style keywords for declaring properties (public
, protected
, and private
) would be unaffected.
Future Scope
Reserving the var
keyword may allow it to be used for new functionality in 8.0 and beyond.
Proposed Voting Choices
As this is a language change, this RFC requires a 2/3 majority to pass.
Patches and Tests
No patch has been created yet.