Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How do I reference an object instance from a class initialization in FuelPHP?
  • What is the correct way to call an object from within a class function? This errors with: ErrorException [ Error ]: Using $this when not in object context:
    <?php
    class Model_Users extends Model
    {
    public static function _init()
    { $mongodb = \Mongo_Db::instance('content'); }
    public static function get_user($name)
    { $user = $this->mongodb->get('users', array( 'name' => $name )); return $user;
    }
    }
  • This worked: <?php
    class Model_Users extends Model
    {
    private static $mongodb;
    public static function _init()
    { self::$mongodb = \Mongo_Db::instance('content'); }
    public static function get_user($name)
    { $user = self::$mongodb->get('users', array( 'name' => $name )); return $user;
    }
    }
  • Don't use "self::", use "static::". There is a small but significant difference between the two, which will become appearant when you start extending classes.

Howdy, Stranger!

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

In this Discussion