How to dynamically connect to class'the properties and methods of another class'and or trait'huh?

All kind time of day. Is it possible to dynamically associate one class with another? The base/parent classes can be somewhat connected/child classes/traits assume a lot. In the parent class basic logic and basic properties, and also determines the name of the plugin/child trait/class. Plug-in/treate child/class with additional properties and some methods, which change the behavior of the parent class. So, is there any method in the base class to connect a trait or another class so that its properties and methods available from the base/parent class?

class Object {
 __construct($name) {
 use $name;
 //this will cause an error, you need some analog
}
}

trait Name {
 public $x = 1;

 public function metod() {
 return 2;
}
}

$object = new Object('Name');
echo $object->x;
echo $object->metod();


Or maybe there's some way to go in reverse order and dynamically set the name of the parent class?

//Something like this...
Name extends $object_name


I guess I'm trying to reinvent the wheel and there are more logical ways.
June 14th 19 at 21:03
2 answers
June 14th 19 at 21:05
Solution
of Name make the class Object implementing it and adding instance methods __get and __call. But it is a hogwash, it is better to think over architecture.

class Object
{
 protected $di;

 public function __construct($di)
{
 $this->di = $di;
}

 public function __get(string $name)
{
 if (property_exists($this->di, $name)) {
 return $this->di->$name;
 } else {
 throw new \Exception('blah');
}
}

 public function __call(string $name, array $arguments)
{
 if (method_exists($this->di, $name)) {
 return $arguments ? $this->di->$name($arguments) : $this->di->$name();
 } else {
 throw new \Exception('blah');
}
}
}

class Name {
 public $x = 1;

 public function metod() {
 return 2;
}
}

$object = new Object(new Name());
echo $object->x;
echo $object->metod();
Thank you! All paths lead everywhere to the decision or the like based on the composition.

On the architecture for more than a week I think of anything more intelligent to come up with failed. Otherwise, everything is very complicated. Not sleeping at night already tired. So I'll start it like it is. Maybe in the process to something more sensible will come. - hank_Wilderman28 commented on June 14th 19 at 21:08
June 14th 19 at 21:07
How to dynamically attach the properties and methods of another class'and or trait'huh?

By means of the language in any way, and change the behavior/structure of the object is bad practice, violating the substitution principle Barbara Liskov. Not fully understand Your problem, but would recommend to look in the direction of aggregation and composition
The behavior of the base class is not changed, and in fact supplemented. If the plug-in classes would be the only properties it would be simple. But there is another plug-in tools which are difficult to call data.

I guess in the end, as your Board, will make the principle of composition, because the class name can be set dynamically and the method names are known in advance. And the properties just written in the current class search. - hank_Wilderman28 commented on June 14th 19 at 21:10
Why does... you Can generate the class description and to execute this code, but...
But this is a clear sign that architecture is SOMETHING wrong.
Would be better if the author described what led him to believe that this decision is a good idea. - hank_Wilderman28 commented on June 14th 19 at 21:13
I agree, my idea was bad from the start. Everything comes from unwillingness to describe the same properties again from a single entity, and even in different places. Can throw the link to what You mean by "generating the class descriptions"? - Justice.Nienow commented on June 14th 19 at 21:16
Can't, because my memory doesn't turn up a single example where this was used (and good). But it can be done. Cm. the eval () function. - hank_Wilderman28 commented on June 14th 19 at 21:19
Yes , an example is found with eval(). There are on the Toaster , the Dynamic creation of classes (PHP)? But it's too perverse decision :/ - Justice.Nienow commented on June 14th 19 at 21:22
And I said somewhere that it is good? I just disagreed with the assertion that a language is not feasible. - hank_Wilderman28 commented on June 14th 19 at 21:25
sorry , I didn't mean it. I just want to find "normal" solution. - Justice.Nienow commented on June 14th 19 at 21:28
Normal solution should appear after you analyze the problem without focusing exclusively on one variant that first came to mind. Quite likely that what you really need (in terms of quality) and what you asked here - different solutions.
Without analyzing the whole perspective is not to say, how to do good. But seeing what is possible with a fairly high certainty say that somewhere you took a wrong turn. - hank_Wilderman28 commented on June 14th 19 at 21:31

Find more questions by tags OOPPHP