Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Best way to override package classes
  • I tried to override a package class (orm\relation) to add a possibility to select specific collumns for relations (compare with post http://fuelphp.com/forums/discussion/comment/7081). What is the best way to override this method/class?

    I created a class Relation and made my changes. I added it in the app bootstrap file to the autoloader, but I didn't work. After that I added it to the autoloader as last line of code in the bootstrap autoloader (after Fuel::init('config.php'); ) but I didn't work since it couldn't find the original class file (for sure - I overriden the autoloader path). 

    My Question now is: How can I override a class of a package AND the package should also use my class?
  • HarroHarro
    Accepted Answer
    There is currently no way to override package classes, unless the package namespace is aliased to global, like for Fuel\Core and Auth. This is not the case with ORM.

    You could try loading the ORM package first, which executes the bootstrap and defines the classes to the autoloader. After that, override the class with a different path.

    Autoloader::add_classes(array(
        'Orm\\Relation'             => APPPATH.'classes/orm/relation.php',
    ));

    This class has to be a copy of the original, it can not extend it (as it can no longer load the original).
  • That's they way I do it at the moment and I was looking for a better way. But thanks for feedback.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion