I have a class:
class anyClass{
public $var1;
var $var2;
}
What's the difference between $ var1 and $ var2?
I have a class:
class anyClass{
public $var1;
var $var2;
}
What's the difference between $ var1 and $ var2?
public $var1
is a public member of the class access modifiers have been added in php5.
The variable declared as var $var2;
has been deprecated since the php4 version. From php5, it was possible to modify the access of the attributes and methods of the class to allow a more performative reach of the objects, through inheritances, access restrictions, polymorphism, among other good practices in the use of PHP language.
It was a way of declaring itself an attribute with public. It has been deprecated since PHP version 5.
Example of public attribute declaration using PHP version 4 or lower (prefix var
).
<?php
classe Caneta {
var $modelo;
var $cor;
}
Currently, to declare an attribute as public, you should use the prefix public
instead of var
, see how the same example would be with PHP 5 or higher:
<?php
classe Caneta {
public $modelo;
public $cor;
}