Injecting classes

02/23/2012

V4 provides a comfortable way to overload core-classes

From time to time, a developer gets to the point where a projects existing code is not working as expected or as required. In this case, the best would be to ignore the products original code and to provide a modified, patched or new solution.
In some scenarios the isn't even possible, but in nearly all cases it comes with a new problem: The modified code is overwritten with each update of product. In addition, you'll often have to justify, that it's really necessary to modify the original product.
Since the Kajona team knows about all those problems, we're proud to introduce a configurable class-loader with the upcoming v4.

Nearly every web-projects seems to requires to change a certain class of the product. In order to be able to change classes within the Kajona-core without touching the original code, you can inject your own classes, overwriting the core-classes. This is done by configuring the Kajona-class loader.

Therefore, a config-file is provided in the folder /project/system/classes; classloader.xml.
In this file you can provide your own implementations for classes loaded by the framework. Whenever a class not yet known to the framework is required, the classloader first searches the classloader.xml for a matching implementation, afterwards the default-classes are loaded.

Example: You want to overwrite the systems class_session, since you want to get an email whenever someone logs into the system.
Therefore you copy the original class_session, modify it and store it, e.g. under /project/system/classes/class_session_mail.php.

In addition, you make the class known to the classloader by adding the class to the classloader.xml. In our case, the file could look like:

<classloader>
  <class>
    <name>class_session</name>
    <path>/project/system/classes/class_session_mail.php</path>
  </class>
</classloader>

If you want to overwrite more classes, add additional class-entries to the file.
Hint: Yes, the class inside class_session_mail.php still has to be named class_session. Otherwise PHP will die with the message "unable to load required class_session".

That's it. You can overwrite exiting core-classes without even touching them. Cool, isn't it?

Filed under: TOP-News en TOP-News de Kajona News en Kajona News de
No comments made till now
Fork me on GitHub