Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Extending core classes
  • Hi, I've encountered strange problem while extending core classes. I'm extending Arr and DB classes:
    class Arr extends \Fuel\Core\Arr {...}
    class DB extends \Fuel\Core\DB {...}
    

    In bootstrap.php I add these two classes to autoloader like so:
    Autoloader::add_classes(array(
     '\Fuel\Core\Arr' => APPPATH . 'classes/arr.php',
     'DB' => APPPATH . 'classes/db.php'
    ));
    

    It works, but if I change '\Fuel\Core\Arr' to 'Arr' or 'DB' to '\Fuel\Core\DB', it won't load them. Why? Thanks for help.
  • You should not use the Core namespace when extending core classes. The way the Fuel core works is that all core classes are aliased from \Fuel\Core to the global namespace (in which your app lives). Any extended classes are defined in the global namespace, which stops the alias from being created, so your extended class will be loaded and used instead of the core class. If you manually alias a core class, you basically creating an endless loop... So it should be:
    Autoloader::add_classes(array(
        'Arr' => APPPATH . 'classes/arr.php',
        'DB' => APPPATH . 'classes/db.php',
    ));
    

    Just like the example that was already present in the app bootstrap.
  • The problem is that
    'Arr' => APPPATH . 'classes/arr.php'
    

    doesn't work. I get following error:
    Class 'Arr' not found in \core\classes\config.php on line 176
    
  • Then you're doing something wrong. the file is called app/classes/arr.php, in lower case? And defined like so:
    <?php
    
    class Arr extends \Fuel\Core\Arr
    {
     public static function _init()
     {
      \Debug::dump('MY ARR IS ALIVE!');
     }
    }
    

    Just coded this up, added this
    Autoloader::add_classes(array(
     // Add classes you want to override here
     // Example: 'View' => APPPATH.'classes/view.php',
     'Arr' => APPPATH.'classes/arr.php',
    ));
    
    to my app bootstrap, and it works like a charm...
  • Are the files in app namespaced to 'Fuel\Core'? They shouldn't be.
  • I've found the source of problem - I was extending Arr class with a "merge" method, not aware that there already is such method, just not documented. Thanks for your help!

Howdy, Stranger!

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

In this Discussion