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
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 :
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
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.
thanks for your answer.
To completelly understand, I have to move my
file, which containt the
class in the APP/CLASSES/airship.php
Inside the class I have to add his own namespace, lets say :
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 :
wrote inside my Urbanairship class the line
the class in my task file
: include airship.php;
call it normally $message =
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.
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)
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)
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.
Add a Comment
It looks like you're new here. If you want to get involved, click one of these buttons!
Apply for Membership
↳ Job Board
↳ Installation & Setup
↳ Tips and Tutorials
↳ Code share
In this Discussion