Introduction to reflection in php
lang_of_article_differ
want_proper_trans
Every php developer at least once tryed to develop its own framewok using such cool words as mvc, active record, ORM, loggers and so on.
Someone get succeed int this task and someone not. In case you wrote your first framework, most probably you should rewrite it almost completely when you're want some new feature or change the behaviour of existing one.
A reason for this is that componens of your framework are highly coupled together and changes in any of it will require changes in dependent parts. When you want to write framework with low coupled components there is few commonly used techincs to achieve this:
write realisation of components based on interface or inheritance, use service locator and or dependency injection when its possible to enable hot swapping of components and so on.
And this is the part where the whole greatness of reflection api comes on. Using reflection api you can do much stuff on the background of your actual code: automatiacclly pull dependent libraries to controllers based on types of libraries, get all interfaces which some class implements, get all names of fields of some class (especially efficient thing when you're planning to write ORM or some kind of automatically generated CRUDs), get information on all functions of class and some on.
Freankly speaking, reflection alows you even more than you expected from it: create instances of classes with private constructors, set value of private properties and of course read them.
Especially thing noticeable about reflection - it allows you to parse and get all doc coments of your classes: class doccoment, properties and methods doc coments. By using a doccoment data you can achieve even more flexibility making your system. For example you can write down in the controller action's doccoment access level rights and ommit extra file with acces rights for all actions. :
/**
* @AllowGroup('admins')
*/
pubic function indexAction() {
There is much to say about reflection in php and i've decided to divide this post to few parts, in the next part we would get all general data about some class