What would be the 'right' way to declare variables in PHP OO?

Asked

Viewed 62 times

4

I’m starting to study O and fall into the following doubt:

<?php

class Caneta {
    var $modelo;
    var $cor;
    var $ponta;
    var $carga;
    var $tampada;
}

OR SO:

<?php

   class Caneta {
      public $modelo;
      public $cor;
      public $ponta;
      public $carga;
      public $tampada;
}

1 answer

5


var is considered obsolete and should no longer be used in new codes, it was a time that created the class but did not want to be another language, but then changed their mind and resolved that it would have all the diverse visibilities of a member that other languages had private, protected and public, so this is used between the two possibilities presented.

This way you make it clear that the variable is public, with the old keyword became public but did not make it clear, because it could not be anything else. Since the philosophy of language has changed, it makes more sense.

There are detractors to this idea, but the official position is this.

Browser other questions tagged

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