Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
FuelPHP and Namespaces
  • I apologize if this has been answered elsewhere, but I've looked through the documentation and forums and I can't seem to figure this out.

    I would like to namespace the classes in my FuelPHP app, using a namespace similar to MyApp\Core. Am I required to make two new directories in APPPATH/classes just to do this?

    For example, if I wanted to create a class called MyClass in MyApp\Core, do I HAVE to put myclass.php in APPPATH/classes/myapp/core/myclass.php?

    The MyApp\Core namespace seems to work fine for controllers and models. I have MyApp\Core\Model_Item in APPPATH/classes/model and it loads and runs just fine when called from a controller using the MyApp\Core namespace and stored in APPPATH/classes/controller. However, when I try to call MyApp\Core\MyClass, I get the error that the class MyApp\Core\MyClass cannot be found. Oddly enough, it says that the error occurred on the last line of APPPATH/classes/myclass.php.

    All files that I want in my namespace have the namespace MyApp\Core; line at the top. I even tried adding 

    Autoloader::add_namespace('MyApp\\Core', APPPATH.'classes/');

    to my application bootstrap, but to no effect. I also tried adding an Autoloader::add_class() call for the MyClass class, but it didn't work.

    Am I missing something, or is it just not possible to use namespaces for classes in APPPATH/classes? If it's not possible, why not?

    UPDATE: Now I'm even more confused. I've tried putting MyClass into the global namespace and the same thing happens. Class not found error on the last line of myclass.php.

    I'm starting to think this might have something to do with my class so here it is. I'm trying to extend FuelPHP's logging to log to a database as well.

  • I'm having more or less the same problems with namespaces as you. I cant get it to work.

    I think the docs do not include this info as it should to be clear. My problem is with a PSR-0 compliant library that I dont know how to add. I manage to add it as add_classes but not the namespaces.

    I hope someone else throws some light over this topic coz I'm nearly the full desperation.
  • Have you tried to create a package instead of putting that in your application?
    As Harro says me some months ago: 

    A module can be seen as a reusable application component, a package can be seen as a reusable core/framework component. The first provides frontend functionality, the second backend.

    If you want to create a package this is how:

    1. Create a folde under "packages" folder. I'll use "mypackage"
    2. Create a bootstrap.php file like this http://pastebin.com/FCBBrCLh
    3. Put your class in the folder "/packages/mypackage/class/myclass.php
    Now to use your class symply import in your project the package and use it with the syntax

    [code]\MyPackage\MyClass::mymethod()[/code]

    Let me know.
  • To add to this,

    The Autoloader supports PSR-0 compliant libraries. In the bootstrap, in the add_namespace() call, add 'true' as third parameter to indicate it's a PSR-0 namespace, and your classes will load fine.

    Technically you can do this in app too, but it will be messy. A package is a lot cleaner.
  • So in the case that I'm using composer... just to clarify this.

    Where should I put my composer packages?

    Right now I'm adding them to APPPATH.'vendor/mypackage/mypackage/src' because I'm using composer in the APPPATH.

    So I have the composer.json in the APPPATH folder. Then if I do composer install it will install the libraries in the "vendor/package/package".

    I imagine that I'm doing this wrong. Any ideas?
  • How you do this depends a bit on whether you use composers autoloader, of FuelPHP's autoloader.

    This article (http://tomschlick.com/2012/11/01/composer-with-fuelphp/) might give you some pointers on working with composer.
  • That was AWESOME thanks I've never found that article before. I never thought it was that easy :O
  • After several more hours of trying various things, including ilNotturno/Harro's suggestion of putting the class in a package, I still couldn't get it to work.

    Finally made a test class that just echoed out some text, and it worked fine. Turns out, either PHP or Fuel doesn't like it when you extend a class in a different namespace. Taking out extends Fuel\Core\Log gets rid of the error and the class works (after switching parent calls to Fuel\Log). Given a stackoverflow question asking about extending across namespaces, I'm thinking it's FuelPHP. Thoughts?

    Out of curiosity, is it bad FuelPHP practice to use namespaced classes in my app?

    "Technically you can do this in app too, but it will be messy. A package is a lot cleaner."
     
    Were you just talking about Composer and other external packages? Because it seems kind of silly to create a package for a few classes that are part of my app.

    Thanks.
  • HarroHarro
    Accepted Answer
    Extending across namespaces isn't a problem at all, I do it all the time. So it must be related to what you do and how you do it.

    There is no real practice, using packages and modules makes stuff more reusable, but if you want them in your app/classes folder, no problem. That's entirely up to you, and Fuel's flexibility lets you do that.
  • Thanks!

    I'm going to have to mess with my class some more, but at least I have it partially working.

Howdy, Stranger!

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

In this Discussion