Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Namespaces ?
  • Hi everyone,

    I have a helper class, which is located in the fuel/app/classes directory.

    This helper class is auto loaded by the config.php, so I am able to use it in the entire project, as it contains common methods.

    Now I have to create some tasks, and I want to use this helper class.

    Because the helper class has no namespace (not needed inside the project), I cannot reach it, and use it.

    If I set the namespace Fuel\Tasks in the helper class, in order to be accessible from the tasks area, it cannot be automatically loaded by the config ... i receive this error :

    Error - Class Helper defined in your "always_load" config could not be loaded. in COREPATH/classes/fuel.php on line 305

    If I comment the class from not beeing anymore auto loaded in config.php, obvious i wont be able to use it in the profject (beeing also namespaced now with Fuel\Tasks)....

    So my question is :
    How can I use the helper class (located in fuel/app/classes) also for my project and also for tasks, keeping in mind that the class should be auto loaded when the fuel starts ?

    Thanks

  • I found the solution through the following steps :

    - set a namespace for the helper class (let's say MyNamespace);

    - comment the class name in the auto_load section from the config

    - auto load the class, through the bootstrap.php
      'MyNamespace\\Helper'  => __DIR__.'/classes/helper.php',

    - in the task i had to say at the top

      namespace Fuel\Tasks;
      use MyNamespace\\Helper;

    and that's all.

    Maybe is useful for others too.

     
  • If your helper class has no namespace, i think you can call him in your task like that : \Helper::method()
  • If the current class is in a namespace, correct. Whenever you want to access a class in another namespace, you have to specify the namespace. Even if that other namespace is "no namespace" (which PHP calls the global namespace).

    This is also true for all PHP internal classes, for example \Exception.

Howdy, Stranger!

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

In this Discussion