Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
\ in front of objects
  • I was just looking codes in Fuel application Stationwagon. There are '\' in front of class/object and such as followings. I am hoping what this \ is. Thanks in advance.
    class Controller_Users extends \Controller_User { ...
    ...
    ... 
    $this->template->content = \View::factory('index');
    
    ...
    if ( \Auth::check())
    ...
     \Session::set_flash('notice', 'FLASH: logged in');
     \Output::redirect('users');
    
    ...
    and more and more..
    ...
    
  • The \ is for accessing namespaces.
    You can find more about them here: http://www.php.net/manual/en/language.namespaces.basics.php So for example \Auth::check() means call static method check() of Auth class from root namespace.
  • Also one very important point: they're in front of classes, not objects. May seem a small difference to some, but is a very important distinction.
  • Thanks guys. The a part of stationwagon's structure is like this.
    app 
       classes
         controller
           ...
           user.php 
    ...
    ...
    modules
       users
        classes
         controller
           users.php
    ...
    ...
    

    In users.php in modules
    class Controller_Users extends \Controller_User { 
    and above codes.
    

    My understanding is that when you extend the (global?) class in app, you need use \ to indicate it is global class.
    Controller_User is global since it is under app folder. Am I right?
  • I'm new to PHP OOP, but I thought if you extend a class you have to have a different name for the child class. So I don't understand. Thanks.
    Jaroslav Petrusevic wrote on 01/29/11 10:33 pm:
    @icf, I think it's mainly because now if we extend core classes like DB,
    and try to access the \DB - we get the access the extended DB class
    and if we use Fuel\Core\DB - we access the 'original' db class, not extended one
  • Read up on namespaces, which is pretty much what this topic is about.
  • Shino, this is a good question. I was wondering if namespaces are indeed used correctly and consistently in FuelPHP code... For instance, in packages/activerecord/classes/model.php we have:
    namespace ActiveRecord;
    
    
    use \DB;
    use \Database;
    use \Inflector;
    
    class Model {
    ...
    
    ...and then, somewhere in a method we have this:
     $this->prefixed_table_name = \Database::instance()->table_prefix($this->table_name);
    

    The first statement means the current namespace is ActiveRecord. First I think something is wrong with the use statements; they are are referring to DB, Database and Inflector classes in the global namespace. But their fully qualified names are \Fuel\Core\DB, \Fuel\Core\Database and \Fuel\Core\Inflector. The same applies for \Database::instance() in the method... I think the correct version is this:
    namespace ActiveRecord;
    
    
    use Fuel\Core\DB;
    use Fuel\Core\Database;
    use Fuel\Core\Inflector;
    
    class Model {
    ...
    ...
     $this->prefixed_table_name = Database::instance()->table_prefix($this->table_name);
     // Here you could also use its fully qualified name:
     // $this->prefixed_table_name = \Fuel\Core\Database::instance()->table_prefix($this->table_name);
    ...
    

    That's how you'd do it in Java, C# or any other strongly-typed language.
    And that's also what PHP name resolution rules are saying. On the other hand PHP is not strongly-typed and you also have class-autoloading... Also, wouldn't be better to put FuelPHP packages in Fuel namespace, so we'd have Fuel\Core\[...] for system classes and:
    Fuel\ActiveRecord
    Fuel\Auth
    ... It makes more sense to me. And if I'm not wrong, that's how it's declared in Octane. In fact, the whole code in Octane uses namespaces correctly... Any thoughts?
  • ...to reply to my own previous post, digging through code I found this in core/classes/autoloader.php:
    ...
     /**
      * @var array List off namespaces of which classes will be aliased to global namespace
      */
     protected static $core_namespaces = array('Fuel\\Core');
    ...
    

    So that answers my question on why \Fuel\Core == \ works. All classes within Fuel\Core are aliased in the global scope. Now I understand. But, just out of curiosity, why did you guys choose to do so? Is it simply to make the framework friendly to developers unfamiliar with namespaces?...
  • @icf, I think it's mainly because now if we extend core classes like DB,
    and try to access the \DB - we get the access the extended DB class
    and if we use Fuel\Core\DB - we access the 'original' db class, not extended one

Howdy, Stranger!

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

In this Discussion