Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Trigger a method from a class located on PKGPATH through a task
  • Hi everyone,
    I have a task, and I need to use a class(Airship) located in PKGPATH:
    How can I do it ?

    inside my task I say :

    require PKGPATH.'Airship.php';

    Inside a method from my task I say
    $x = new Airship;

    And it gives me an error :

    'Class 'Fuel\Tasks\Airship' not found in APPPATH/tasks/fantomicoptaimport.php on line 448'

    I tried to copy the Class in the Tasks folder, and put inside the Airship class the line

    namespace Fuel\Tasks;

    the same as I have in my main task.

    I receive the same error !

    What am I doing wrong, and which is the solution, please.

  • A class should be in a 'classes' folder. Then the autoloader can find it and load it, no need for manual loading. In PKGPATH (which is the root of installed packages) is definitely a bad idea.

    You get that error because tasks are defined in the Fuel\Tasks namespace, and since you try to instantiate a class without specifying the namespace, PHP will try to find it in the current namespace.
  • Hi Harro,

    thanks for your answer.
    To completelly understand, I have to move my airship.php file, which containt the Urbanairship class in the APP/CLASSES/airship.php

    Inside the class I have to add his own namespace, lets say :

    namespace Airship;

    Then inside my task I have to call the class like this -.

    $message = new Airship\Urbanairship;

    It's correct, because I've tried and it doesn't work.

    The only solution till now was to :

    1. wrote inside my Urbanairship class the line namespace Fuel\Tasks;
    to include the class in my task file : include airship.php;
    call it normally $message = new Urbanairship;

    Please let me understand the correct way to do the job, because I want to use the class from a task, but also from a controller.

  • HarroHarro
    Accepted Answer
    FuelPHP rules say that:
    - all classes go into the classes folder (for app, modules and packages)
    - the combination of namespace and classname should map uniquely to a file
    - packages and modules have a base namespace equal to the package or module name

    The mapping works as follows: if the namespace location is known (i.e. it's a package or a module, that will select the prime location of the classes folder). That namespace is then stripped. If not, it defaults to app/classes. The remainder (any remaining namespaces and the class name) are glued together using a directory separator, and then all backslashes and underscores are replaced by directory separators.

    So, if you have a namespace "Airship" and a classname "Urbanairship", it can be in:
    - app/modules/airship/classes/urbanairship.php (if airship is a module)
    - packages/airship/classes/urbanairship.php (if airship is a package)
    - app/classes/airship/urbanairship.php

    If your class would be called "Urban_Airship", it would be:
    - app/modules/airship/classes/urban/airship.php (if airship is a module)
    - packages/airship/classes/urban/airship.php (if airship is a package)
    - app/classes/airship/urban/airship.php

    If you deviate from this rule, the autoloader won't be able to load it without help. If you have to deviate from it (which is not recommended but sometimes needed, for example because it's a third-party class), you can tell the autoloader it exists, and where to find it, in your app/bootstrap.php:

            'Airship\\Urbanairship' => APPPATH.'classes/some/other/dir/otherfile.php',

    So you define the fully namespaced classname, and tell the autoloader which file defines this class. You can do the same in a package bootstrap. You can not do this for modules.
  • Thanks for your complete answer.
    I understand now.

Howdy, Stranger!

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

In this Discussion