2
Assuming that in situation X I want to return a class instance through variables, I see no problem in 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 wouldn’t 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 use this class with the instances as an extensible class, since it will contain the instances of what is being done.
In what I’m doing, it’s working right, but it seems strange to me,I want to know if it’s a good idea to do this (have instances as an object) from the point of view of maintenance and performance of the code?
In my view it does not seem to be good practice, but functional, if I am correct in that (in not being good), as it would be a good way to get the same result ?
NOTE: Doubt is about having the instance of one class as the object of another.
The PHP constructor has no return, by default, so the
return
there has no effect. You have read about the design pattern Factory?– Woss
@Andersoncarloswoss I know how it works, but I don’t know if this is the context, I think I expressed myself half bad there, I changed the statement a little, the Factory would serve to replace the way I instate the class, but I’m doubtful about keeping the instance as an object, as in $this->Inst = new somemore();
– AnthraxisBR
@Anthraxisbr, that you did (the class
DevolverInstancia
) seems a kind of indirect that can even simulate the functioning of C pointers in other languages. Only in the constructor of it are you doing an introspection that differs from the "conventional" structure of an indirect, which would simply store the instance. Your doubt for me remains unclear– Jefferson Quesado
@Jeffersonquesado I think it’s now clearer the question
– AnthraxisBR
Ok, you want a complex object. Eventually you will need to access your fields and rescue the internal data. This kind of goes against what I understand about encapsulation, but so far, nothing more, perfectly plausible. Having objects inside objects is desirable within some contexts. This is called the composition of objects. If the class that encapsulates the creation of objects within a context only serves to encapsulate creation, it is basically an anemic object. There are several examples in other languages, which use classes/stereotypes/objects.
– Jefferson Quesado
@Jeffersonquesado knows some framework or qlqr thing in php or python that uses something like that for me to take a look ?
– AnthraxisBR
It serves Java no? I’m looking here in the most recent questions but I’m not finding it. I’ll even search the answers @Andersoncarloswoss that there is a lot of cool stuff there
– Jefferson Quesado
@Jeffersonquesado can be, it will be a little more difficult to understand but already help, I do not know the name of this to know what to look for
– AnthraxisBR
@Anthraxisbr, Django uses, I just remembered. When you are describing a database model, it asks you to describe the various object types of the database. When there is a relation with other tables (not Many-to-Many relation), you create a foreign key pointing to the class describing the other object. You can search further by [tag:Django]; I found this question here that seems reasonable for your study: https://answall.com/q/272409/64969
– Jefferson Quesado