N
N
Nikita Samokhvalov2015-05-21 10:07:08
PHP
Nikita Samokhvalov, 2015-05-21 10:07:08

How to infiltrate a class?

I am developing for one not very flexible framework the functionality of the so-called. "plugins". I will not describe the functionality in its entirety, I will immediately specify the problem.
Primitive scheme. There is a Component class (the core of the framework). There is a trait PluginTrat (my trait, I can write anything) that is included in the component:

class Component
{
     use PluginTrait;

     function executeComponent()
     {
          …
     }
}

trait PluginTrait
{
     …
}

In a trait, without reassigning the executeComponent() method, I need to inject into the component class and call my trait methods before and after the executeComponent() method is executed. That is, the execution steps of the component class should be as follows:
1. Trait method1.
2. component method executeComponent() (well, it will be executed anyway).
3. method of Trait2.
The problem is that the Component class is the core of the framework, and for certain reasons I cannot change (implement) it. In addition, you need to take into account that the class inheritance chain can be increased, and child classes will have their own executeComponent () method (its presence should not violate the class execution order described above).

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
Gregory, 2015-05-21
@difiso

Example #2 , right?

<?php
class Base {
    public function sayHello() {
        echo 'Hello ';
    }
}

trait SayWorld {
    public function sayHello() {
        parent::sayHello();
        echo 'World!';
    }
}

class MyHelloWorld extends Base {
    use SayWorld;
}

$o = new MyHelloWorld();
$o->sayHello(); // Hello World!
?>

A
Alexey, 2015-05-21
@Leshgan

class myComponent extends Component
{
function executeComponent()
     {
       ...  
       parent::executeComponent();
       ...
     }

}

So?

V
v1os, 2015-05-21
@v1os

habrahabr.ru/post/106426 so it is possible, if you pervert a bit. Observer pattern is well described in books

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question