Why use VAR in PHP?

Asked

Viewed 1,539 times

9

Why use VAR in php if we can already declare variables without VAR?

We can do this: $teste then why do it? var $teste

For example, it’s the same thing I do?

class Caneta {
    var $modelo;
    var $cor;
}

And that:

class Caneta {
     $modelo;
     $cor;
}

1 answer

10


TL;DR

Do not use var to declare class properties this was used in php4.

The keyword var was used in php4 to define a property in a class. From php5 its use was advised instead one should use some of the access modifiers: public, protected or private.

In the example code is the same thing both properties will be accessible by all or is equivalent to the public. By default if the access modifier is omitted its visibility will be public That goes for methods too.

Recommended reading:

Property x Attribute

  • Look at my issue, rray, please? :)

  • 1

    @Lucascarvalho complemented the answer.

  • Thank you rray, just summing up, is it the same thing to use any of the 2? In the case on PHP5

  • @Lucascarvalho is the same thing, the ideal is not to use var same. In the manual it mentions that from version 5.0 to 5.1.2 was generated a Warning after no more. manual

  • rray, you know why in Netbeans IDE it’s a mistake if I don’t use var?

  • Look? http://prntscr.com/fhu95j

  • 1

    @Lucascarvalho is warning that you should use some access modify. It suggests that it is more readable to use some but in this case no error is generated, if I am not mistaken you can turn off some rules of netbeans code checking.

Show 2 more comments

Browser other questions tagged

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