Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Calling Classes via Variable
  • It seems I can't do this: $class = 'Model_Something';
    $result = $class::method(); I get an error saying the class in question doesn't exist. But calling the class w/out going through a variable returns the desired result.
  • Thanx for clearing up this, I can now remove the dents from my forehead ;)
  • It's actually not a bug in PHP, whenever you use a string to refer to a class it must be the full classname including the namespace. The current namespace is never taken into account. This is quite logical if you think about it, that way a string that gets passed around will always have the exact same meaning - no matter where you use it for a call. The example on PHP.net is not wrong, but it is ambiguous. It includes the first example "include 'example1.php';" and that's what the first few calls use, they should have named that class different from the namespaced one for clarity. The latter examples all show that it doesn't matter whether you prefix the string with a backslash or not. It will start in global regardless.
  • That is odd, as of PHP 5.3.0 you should be able to do that (see http://php.net/manual/en/language.oop5.static.php). What is the exact error you're getting? Are you it isn't something else, like for example a namespace issue? What does this tell you?
    \Debug::dump(class_exists($class, false));
    
  • Says the class doesn't exist. It is namespaced properly, and trying to call the class w/out the variable works fine.
  • The exact error:
    ErrorException [ Error ]: Class 'Model_Analytics' not found The debug dump:
    bool(false)
  • Well, if class_exists() returns false, it's clearly not loaded. If you do this, and it does exist (and can be found), the autoloader should load it and the debug should return true:
    \Debug::dump(class_exists($class));
    

    Where (in what namespace) is this class defined, and from where are you calling it? If the class is not in the current namespace, you will have to add the namespace to the variable. And don't forget, if you do, you need to specify double backslashes...
  • I'm in a module context. When I call a class straightup, it works. When I call the same class via variable, it doesn't work.
  • Had some time to test this. You are right. When calling the class directly, PHP knowns the current namespace, and will load the class succesfully from that namespace. When you use a variable, that doesn't work, you HAVE to include the namespace in the variable.
    $class = "\\Modulename\\Model_Something";
    // or if you don't want to hardcode it
    $class = "\\".__NAMESPACE__."\\Model_Something";
    

    Not sure this is intentional, or a bug in PHP...
  • When you use the variable, the request to load the class arrives at the autoloader without the correct namespace prefixed. I tested this with a test class in the same file, which is obviously in the same namespace, and loaded, and even then the autoloader is called, indicating PHP want's to load the class from the global namespace, not from the current namespace. Odd, because http://www.php.net/manual/en/language.namespaces.dynamic.php gives an identical example (#2). So I settle for a bug in PHP.

Howdy, Stranger!

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

In this Discussion