What’s the difference between var $var and public $var?

Asked

Viewed 210 times

4

I have a class:

class anyClass{
   public $var1;
   var $var2;
}

The difference between $var1 and $var2?

3 answers

6


public $var1 is a public member of the class access modifiers were added in php5.

Already var $var2 is a way to declare an attribute of a legacy class of php4 and should not be used in new projects, it still works in php5 but its visibility is public.

Manual - visibility

  • Ah ta.. ta in the documentation.. Today I am looking for.. I passed my eye very quickly and did not find in Visibility talking about var. I’ll give you some more research.

  • Interesting is that the documentation warns that, from version 5.1.3, use var would cause the invocation of a E_STRICT. However, in all the tests I performed, this did not happen. @_@ ???

  • 1

    @Wallacemaxters, this happens in versions 5.x up to 5.1.3, after this not generated any Warning, it is something like the E_ALL and the E_STRICT(inserted in php5.3), has another function that I do not remember that only generates Warning in php5.1 ... vc can test different versions here only php5.0.4 is offline.

  • @Wallacemaxters, this is question about the function you talked about, is_a.

  • 1

    I found that it is not possible to use var Static. It would cause a Parse error: syntax error, unexpected 'static' (T_STATIC), expecting variable (T_VARIABLE)..

  • It is because in the case of a variable static, you can "omit" the visibilitade. Example static $x = 1, without the var

Show 1 more comment

0

It was a way to declare an attribute with public. It is out of use since PHP version 5.

Public attribute statement example using PHP version 4 or below (prefix var).

<?php
    classe Caneta {
        var $modelo;
        var $cor; 
    }

Currently, to declare an attribute as public, you must use the prefix public instead of var, see what the same example would look like with PHP 5 or higher:

<?php
    classe Caneta {
        public $modelo;
        public $cor; 
    }

0

The variable declared as var $var2; is being discontinued since the php4 version. From php5, it was possible to modify the access of class attributes and methods to enable a more performatic range of objects, through inheritances, access restrictions, polymorphism, among other good practices in the use of PHP language.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.