1

Is there a way in PHP to wrap child function in parent class? I have cron job commands, and want to add one method at the end of every command execution. something like this:

public class child extends parent {

    public function run($args) {
        //something here
    }
}




public class parent extends CConsoleCommand {

    public function run($args) {
        child_run_function($args);
        $this->sendExecutionStatusEmail();
    }

    public function sendExecutionStatusEmail() {
        //some code here
    }

}
dzona
  • 3,323
  • 3
  • 31
  • 47
  • Check this link, maybe is of help ?http://stackoverflow.com/questions/9525208/php-wrap-all-functions-of-a-class-in-a-subclass – Ahmed Ziani Mar 26 '15 at 10:53
  • @AhmedZiani this example is oposite of what I want to do – dzona Mar 26 '15 at 12:07
  • Instead of child extending parent, it may be easier to use 'composition' and inject the 'child' in the _parent_ constructor. Then when you call _`parent->run(args)`_ it will execute the _`child->run(args)` then do your processing. I have not looked at the 'run' method so this is just some thoughts about how would investigate it. – Ryan Vincent Mar 26 '15 at 13:59
  • I suggest you to check how works yii active record class. When you save a record, calling save method, it trigger parent methods like filters, validate, before and after save without any implementation on child class – Alejandro Quiroz Apr 01 '15 at 02:42

1 Answers1

0

The best way to approach it would be something like this:

class Child extends Parent {

    public function run($args)
    {
        // Child specific code

        // Call the parent's run function
        parent::run($args); // Note: parent here does not refer to a class parent, it's a PHP keyword which means the parent class
    }

}

class Parent extends CConsoleCommand {

    public function run($args)
    {
        $this->sendExecutionStatusEmail();
    }

    public function sendExecutionStatusEmail()
    {
        // Something here
    }

}

Edit

Or what you could do is write some functionality into the parent run function that checks for the existence of a function and calls it before triggering the e-mail. Something like:

class Child extends Parent {

    public function yourMethodName()
    {
        // Do something
    }

}

class Parent extends CConsoleCommand {

    public function run($args)
    {
        // If the method yourMethodName exists on 'this' object
        if (method_exists($this, 'yourMethodName'))
        {
            // Call it
            $this->yourMethodName();
        }

        // The rest of your code
        $this->sendExecutionStatusEmail();
    }

    public function sendExecutionStatusEmail()
    {
        // Something here
    }

}

This way you can create a method that matches yourMethodName on the child class and when the run method is called (Which will be inherited from the parent), yourMethodName will be called if it exists.

Jonathon
  • 15,873
  • 11
  • 73
  • 92
  • yes, but I want to wrap child functions without changing them – dzona Mar 26 '15 at 12:09
  • @dzona See my updated answer, does that help at all? – Jonathon Mar 26 '15 at 12:44
  • have you noticed that both child and parent have same method name called ``run``? Your code will make endless recursion for example I wrote :) Even if you call child method properly, using ``ReflectionClass`` it will again make killer loop. Since this example is in ``Yii`` framework, I could probably override console runner and add custom function after command execution, but I wan't to discuss with you, is there a pure PHP solution for solving this problem – dzona Mar 26 '15 at 13:32
  • I don't think so. If you omit the `run` method in the child, it will inherit the parent class version of run which is what you want. If you decide to override the run method in your child class you can. But if you want to keep the functionality of the parent class you can call `parent::run()` somewhere within your implementation. I'm not too familiar with Yii but this should hopefully work for you. – Jonathon Mar 26 '15 at 13:47