PHP class method injection without experimental functionality

I was discussing with Pippin from PippinsPlugins about using his pretty cool, extremely light weight logging class for YAPS, by extending the core.

The conversation got me thinking about ways to inject methods into an already existing class without using experimental code and extensions for PHP, such as classkit and runkit.

It would be great if the YAPS core could be extended in such a way that extensions could act as part of the core. An example would be Pippins WP Logger class. How could this be added to the core, without the core developer (myself), writing and maintaining the code base?

I have come up with an idea, and it’s just that, an idea. I haven’t tried what I am about to explain and haven’t looked into potential implications – but that is something I will do before implementation.

The idea is actually pretty simple and would involve PHPs magical setters and getters as well as WordPress actions.

The basic principle would be to write into the YAPS core class the ability to set an external class instance that would be accessible from the core itself, thus making the external class available within the core and related YAPS classes by using a magical getter to call the class.

1
$yaps->setWPLogger('WPLogger')

The external class would need to accept and set the yaps core class instance (making in available in the external class). This can be done by the extension developers with a simple __construct method.

1
2
3
4
public function __construct( YAPS $yaps )
{
    $this->yaps = $yaps;
}

Based on WP Logger, the getter could be along the lines of of the following, which would return the logger instance.

1
$yaps->getWPLogger()

WordPress actions could be used to setup the injection of the accessible class from within YAPS.

Conclusion

Like I said, this is just a thought and for now will be added to my list of ideas for YAPS. Currently of which there are plenty of.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>