Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Undefined constant while trying to access package class
  • Hi,

    Recently I have created my own package, which is handling the authorization (I don't want to use Fuel's auth packages, because of my database structure).

    So I am initializing it in the config.php:

            'packages'  => array(
                'parser',
                'myauth',
            ),

    And whether I am trying to access any of the class function:

    $auth = \MyAuth;
    $auth->login($login, $password, $hash);

    I am getting the following error exception:

    ErrorException [ Fatal Error ]:
    Undefined constant 'MyAuth'

    Its working, when I do:

    $auth = new MyAuth();
    $auth->login($login, $password, $hash);

    But I don't want to start a new instance but would like to access the instance loaded by the core (config.php configuration).

    I have also tried to set to always_load the class:

            'classes'  => array(
                'session',
                'myauth',
            ),

    Where is the problem?
  • philipptempelphilipptempel
    Accepted Answer
    If MyAuth is your package, then \MyAuth is the namespace and thus an undefined constant for PHP since it's assuming a class name when you do $auth = \MyAuth.

    You want ensure following the singleton approach for your auth package, so you need at first a class \Myauth\Auth with its static _init function to set the singleton instance. Then, you may get access to your auth instance via \Myauth\Auth::instance() or \Myauth\Auth::$instance if the property is public.

    Take a look at other, simpler classes like \Fuel\Core\Theme which also handle singleton instances so you'll get what I'm talking about ;) The auth package might not be a good example for understanding singleton in Fuel since it uses some crazily awesome driver-loaded class layout, and it anyway is core-namespaced which is why you an use e.g. \Auth::get_user() instead of \Auth\Auth::get_user() (here, the first \Auth is the namespace, the second \Auth the class being accessed).

    Hope that was to be understood, otherwise ask here or (to get quicker responses) on #fuelphp :)

    PS: Class and namespace names should following naming convention which is ucwords, that's why I was using Myauth over MyAuth in the example.

Howdy, Stranger!

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

In this Discussion