Skip to content
Advertisement

PHP static method call with variable class name and namespaces

I’m trying to call a static method for a namespaced class from another class with the same namespace. But the other class’ name is contained in a variable :

JavaScript

Here is the Book class :

JavaScript

My type variable contains a valid class name here Book. This class is in the same folder, and uses the same namespace. This code returns the error Class 'Book' not found. I have tried several variations (from the SO questions I found) using backslashes, or the call_user_func function, but nothing worked. Anyone knows what’s wrong ?

Advertisement

Answer

When using a variable to reference your class, you need to use a fully qualified name. Try this…

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement