Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Model Namespace Question
  • I think i may just not understand namespaces or something here very well.
    But how come I can't use a different namespace rather than Model in my models?
    So here is my simple test functions.
    <code>
    namespace <b>Models</b>; class About extends \Model
    {
    public static function get_count()
    {
    $count = 56; //Will get this from the DB SOON.
    return $count;
    }
    }
    </code>
    and the controller
    <code>
    Class Controller_About extends Controller
    {
    public function action_index()
    {
    $result = <b>Models</b>\About::get_count();
    echo "i'm thirsty " . $result;
    } }
    </code>
    If I change all my references to be namespace Model; and Model\About, everything is fine.
    Do all Models have to lie in the Model namespace?
  • There is no such thing as a "Model" or "Models" namespace, unless you have created one. The FuelPHP autoloader is quite intelligent where it comes to mapping classes to files in the filesystem. It will combine the namespace and the classname, and converts namespace separators and underscores to file system separators. This means that "\Model_About" (global namespace, class 'Model_About') and "\Model\About" ('Model' namespace, class About) will both resolve to the same file /classes/model/about.php (which should then contain the correct class name). So it's up to you how you want to access your classes, from the global namespace or namespaced.
  • I think i'm following now, its all about filename resolution using the class/namespaces.
    so I could use
    class Model_About extends Model {...}
    and call it using \Model_About::get() from the controller
    or
    use namespace Model;
    class About extends \Model{...}
    and call it using Model\About::get() from the controller And in my above example if i created a directory /fuel/app/classes/models
    i could put my class About in there. Thanks for the reply!

Howdy, Stranger!

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

In this Discussion