PHP Using Magic Methods or not?

Asked

Viewed 2,865 times

6

A doubt arose that I find interesting and would like to post here.

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

Use like this:

public function setNome($nome)

Or with magical methods? Like this:

public function __set($attr,$valor)

I ask this because I see in other people’s codes, despite having magical methods many do not use.

1 answer

3

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

<?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();

Realize that the private $id and private $nome are being targeted with the magical methods and in getId() and getNome() I’m taking the values, I mean, you can work together to allow this kind of implementation.

Example: Ideone


Another important factor is that you can work with this code set in the pattern Fluent thus;

<?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

Soon after the method is called setId is called the setNome (successively if you have more methods Fluent), which is the logic of this pattern. So it has a flexibility that the magical methods do not provide you, but the public methods set may have.

As reported nothing prevents the joint work of these features in PHP. They are magical methods:

References:

  • 1

    In the past, magical methods implied a loss of performance, but I don’t know if this is still relevant today. It’s?

  • @bfavaretto, I believe not for the fact that frameworks use too much, but, I can not say in relation to performance I have to do a search, if you have any article and wanted to indicate a trusted site we write this post will enrich, But you’re the first person I’ve seen talking about it.

  • 1

    I don’t remember any references in my head, but I’ll look them up too and if you find them I’ll tell you.

  • 1

    I found a few things, but for now I just looked over. It seems that Magic properties and methods do have worse performance, but the impact of this will always depend on the application (ie when/how much are used). Links: http://stackoverflow.com/q/3330852/825789, http://stackoverflow.com/q/6184337/825789, http://stackoverflow.com/q/3634748/825789, http://www.garfieldtech.com/blog/magic-benchmarks.

  • 1

    There’s also this one: http://stackoverflow.com/questions/2697922/are-magic-methods-best-practice-in-php @bfavaretto, there’s no technical factor yet, but if I find it or you find it we’ve added!

Browser other questions tagged

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