mercredi 26 avril 2017

Getters naming convention

I understand the traditional naming convention for getter methods:

getName();

getAddress();

getAge();

But i dont understand why. I would rather just use the property name as the method as in the following example:

class Person
{
    private $name;

    private $address;

    private $age;

    public function __construct($name, $address, $age)
    {
      $this->name = name;
      $this->address = address;
      $this->age = age;
    }

    public function name()
    {
        return $this->name;
    }

    public function address()
    {
        return $this->address;
    }

    public function age()
    {
        return $this->age;
    }
}

This makes code more fluent to me

i.e. $person->address()

But i get pulled up on code reviews when doing this for not using proper naming conventions.

Is there anything fundamentally wrong with not using get/set prefixes ?

Aucun commentaire:

Enregistrer un commentaire