Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
FuelPHP Packages and autoloading
  • I have a custom package "MyPackage" with a bootstrap.php and a folder classes. In this folder, there are some classes, for example "MyClass".

    In the app, I now want to use the class "MyClass" without a namespace. Something like this:

    $obj = MyClass();

    How can I achieve this? I have already added this line to the bootstrap.php of the package:
    Autoloader::add_namespace('MyPackage', __DIR__.'/classes/');
    Autoloader::add_core_namespace( 'MyPackage', true );

    And the second question:
    How can I override a core class - for example  \Fuel\Core\Error - in a package?

    For this, I have added this:
    Autoloader::add_classes(array(
        'MyPackage\\Error'             => __DIR__.'/classes/error.php',
    ));
    But it doesn't work
  • Ok, I think the problem is with the error class, since it works for \Fuel\Core\Log
  • You have to add the package namespace to the core namespace. See the bootstrap of the Auth package as an example.

    All core classes are aliases to the global namespace. You override a core class by changing this alias, to it points to your new class:

    Autoloader::add_classes(array(
        'Error'             => __DIR__.'/classes/error.php',
    ));

  • I have tried this, but it seems not to work. I have extended the Error class like this:

    namespace MyPackage;
    class Error extends \Fuel\Core\Error { ...}

    And in the bootstrap.php of the package:
    Autoloader::add_namespace('MyPackage', __DIR__.'/classes/');
    Autoloader::add_core_namespace( 'MyPackage', true );
    Autoloader::add_classes(array(
    'Error'             => __DIR__.'/classes/error.php',
    ));

    But fuel uses the default error class
  • Chances are that by the time it gets to loading the package, the Error class is already loaded. Which is why the documentation states you should do this overloading in your app bootstrap.

    What does a

    var_dump(class_exist('Error', false));

    give you in your package bootstrap? If true, then Error is already loaded and aliased.

    You could try a

    Package::load('mypackage'); from your app bootstrap, and see if that works?

Howdy, Stranger!

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

In this Discussion