Add sub-property dynamically

1
class ObjetoSimples{
   var $nome;
   var $cargo;
   var $turno;
}

class ObjetoComposto{
    var $nome;
    var $setor;
    var $equipe;
}

How to add new nome with turno and cargo on equipe ?

For example:

ObjetoComposto->novo($this->nome,$this->setor,$this->equipe->novo(nome,cargo,turno))
    
asked by anonymous 04.06.2016 / 09:56

1 answer

1

To do as an associative array is really a solution, even because an object does not stop being one. I agree that it is a beautiful game and should be avoided unless it really makes sense.

So just create the object the traditional way. First it creates an auxiliary object, it initializes each property, and the one that will have an object will use this auxiliary object. The basics are like this, there's no way.

Another technique is to create a constructor in the class to make it easier to create the object. As the default PHP constructor is very limited and would create other problems in the code I prefer (although this would not be ideal in another language) to create static functions that work as constructors, thus frees the constructor without parameters and allows a kind of overloading constructors.

So I created a constructor that gets all the arguments needed to populate all the properties, including an object that will be created in this "constructor."

I've created another constructor that takes only the arguments to populate the properties and requires your code to create an instance of the property that is an object. In this case, to facilitate I have also created a "constructor" to create this object.

In another language this could be done more smoothly.

I do not know if this is the best way because I do not do OOP in PHP, but it's the most obvious one for me.

class ObjetoSimples {
    var $nome;
    var $cargo;
    var $turno;
    public static function construtor($nome, $cargo, $turno) {
        $obj = new ObjetoSimples();
        $obj->nome = $nome;
        $obj->cargo = $cargo;
        $obj->turno = $turno;
        return $obj;
    }
}

class ObjetoComposto {
    public static function construtor($nome, $setor, $nome2, $cargo, $turno) {
        $obj = new ObjetoComposto();
        $obj->nome = $nome;
        $obj->setor = $setor;
        $obj->equipe = new ObjetoSimples();
        $obj->equipe->nome = $nome2;
        $obj->equipe->cargo = $cargo;
        $obj->equipe->turno = $turno;
        return $obj;
    }
    public static function construtor2($nome, $setor, $obj2) {
        $obj = new ObjetoComposto();
        $obj->nome = $nome;
        $obj->setor = $setor;
        $obj->equipe = $obj2;
        return $obj;
    }
    var $nome;
    var $setor;
    var $equipe;
}

$obj = new ObjetoSimples();
$obj->nome = "joao";
$obj->cargo = "gerente";
$obj->turno = "noite";
$obj2 = new ObjetoComposto();
$obj2->nome = "maria";
$obj2->setor = "fabrica";
$obj2->equipe = $obj;
$obj3 = ObjetoComposto::construtor("maria", "fabrica", "joao", "gerente", "noite");
$obj4 = ObjetoComposto::construtor2("maria", "fabrica", ObjetoSimples::construtor("joao", "gerente", "noite"));

See working on ideone and no CodingGround .

    
04.06.2016 / 14:34