What is the 'natural' way of returning one instance of a class as an object of another through a constructor

1

Assuming that in situation X I want to return a class instance through variables, I see no problem doing this:

$rs = "\" .$Namespace . "\" . $Class;
$inst = new $rs();
return $inst;

In an example, where I want to have the option of using a class object in another class to modify something that would not be modified by default, something like an abnormal thing, then I would have this:

class Dados{

   public function dado1(){
      return [1 => 'dado1',2 =>'dado2',3 =>'dado3'];
   }

   public function dado2(){
      return [4 => 'dado4',5=>'dado5',6 =>'dado6'];
   }

}    
class Componentes {

   public function junta($array1,$array2){
       return array_merge($array1,$array2);
   }

   public function transformaEmJson($array){
      return json_encode($array);
   }

}
class DevolverAsDuasClassesComOConstrutor {

    public $Componentes;
    public $Dados

    public function __construct() {

        $inst1 = new Dados();
        $this->Dados = $inst;
        $inst1 = new Componentes();
        $this->Componentes = $inst;
    }

}

$class = new DevolverAsDuasClassesComOConstrutor();

$dado1 = $class->Dados->dado1();
$dado2 = $class->Dados->dado2();
$arr = $class->Componentes->junta($dado1,$dado2);
$json = $class->Componentes->transformaEmJson($arr);
echo $json;

The idea is to use a class without methods, to return other instances, and to use this class with the instances as an extensible class, since it will contain the instances of what is being done.

asked by anonymous 06.03.2018 / 00:52

0 answers