0

I have a doubt what is the difference between below class.

class Test {

    var $location;

    public function __construct() {
         $this->location = 'India';
    }
}


class Test {

    protected $location;

    public function __construct() {
         $this->location = 'India';
    }
}

Why we use var ? What is the purpose of using var as global here.??

Please clarify me.

Alice
  • 25
  • 2
  • 9
  • Basically, it looks like it was deprecated for a while but now is just a synonym for "public" – Cave Johnson May 19 '16 at 06:37
  • *Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning.* http://php.net/manual/en/language.oop5.visibility.php – Hanky Panky May 19 '16 at 06:40

1 Answers1

1

The keyword before variable name defines the visibility of the variable. It defines the access rights for the particular variable.

var

When using var, it will be publicly accessible through out your project same as public.

protected

When using protected, variable is only accessible for the classes which extends parent class for particular page only.

Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning.

Read More Here

Ali
  • 1,408
  • 10
  • 17