Skip to content

New sniff: change properties declared with var to use public #7

@jrfnl

Description

@jrfnl
Sniff basics -
Fixable for PHP: 5.0+
Sniff type: Modernize
Fixer type: Safe (review recommended)

Short description of the sniff

Prior to PHP 5, the var keyword had to be used to declare class properties and all properties were public. As of PHP 5.0, there is a choice between declaring properties as public, protected or private.

Related PHPCompatibility sniff(s):

  • N/A

PHP manual references:

  • In order to maintain backward compatibility with PHP 4, PHP 5 will still accept the use of the keyword var in property declarations instead of (or in addition to) public, protected, or private. However, var is no longer required. In versions of PHP from 5.0 to 5.1.3, the use of var was considered deprecated and would issue an E_STRICT warning, but since PHP 5.1.3 it is no longer deprecated and does not issue the warning.

    If you declare a property using var instead of one of public, protected, or private, then PHP 5 will treat the property as if it had been declared as public.

    http://php.net/manual/en/language.oop5.properties.php

  • http://php.net/manual/en/language.oop5.visibility.php

Example code:

Detect the following code pattern(s):

class OldStyle {
    var $myproperty = 'abc';
}

And fix these to:

class OldStyle {
    public $myproperty = 'abc';
}

Notes for implementation of the sniff:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions