PHP Use Magical Methods or not?

6

There is a doubt that I find interesting and would like to post here.

How do you usually use methods whose functionality is to set and retrieve attributes in a class ?

They use this way:

public function setNome($nome)

Or with magic methods? So:

public function __set($attr,$valor)

I ask this for the fact of seeing in other codes, despite having many magic methods do not use.

    
asked by anonymous 18.06.2014 / 18:00

1 answer

3

Nothing prevents you from using both forms, this can even be of great value. When I use PHP , I use get/set and the name of the fields, like this:

<?php
    class Cliente {
        private $id;
        private $nome;
        public function __construct($id = 0, $nome = NULL){
            $this->id   = $id;
            $this->nome = $nome;
        }
        public function getId(){
            return $this->id;
        }
        public function getNome(){
            return $this->nome;
        }
        public function setId($value){
            $this->id = $value;
        }
        public function setNome($value){
            $this->nome = $value;
        }

        public function __set ($name,$value){
            $this->$name = $value;
        }
        public function __get ($name){
            return $this->$name;
        }
    }


    $cliente = new Cliente();
    $cliente->id   = 1;
    $cliente->nome = "Fulano 1";

    echo $cliente->getId() . " " . $cliente->getNome();

Notice that the private $id and private $nome are being set with magic methods and in getId() and getNome() I'm taking values, that is, they can work together to allow this type of implementation.

Example: Ideone

>

Another important factor is that you can work with this code of set in the default Fluent in this way;

<?php
    class Cliente {
        private $id;
        private $nome;
        public function __construct($id = 0, $nome = NULL){
            $this->id   = $id;
            $this->nome = $nome;
        }
        public function getId(){
            return $this->id;
        }
        public function getNome(){
            return $this->nome;
        }
        public function setId($value){
            $this->id = $value;
            return $this;
        }
        public function setNome($value){
            $this->nome = $value;
            return $this;
        }

        public function __set ($name,$value){
            $this->$name = $value;
        }
        public function __get ($name){
            return $this->$name;
        }
    }


    $cliente = new Cliente();

    $cliente->setId(2)
            ->setNome("Fulano 2");

    echo $cliente->id . " " . $cliente->nome;

Example Ideone

>

Shortly after being called the setId method, you call setNome (successively if you have more methods Fluent ), which is the logic of this pattern. So there is a flexibility that magic methods do not provide, but public% methods can have it.

As reported, nothing prevents you from working together on PHP. They are magical methods:

References:

19.06.2014 / 00:22