1

I'm building a PHP project and I came across this problem I want to define a class by a string with the name of it and as far as I saw online I can do that but I'm having a problem doing that here's the code that doesn't work:

require_once Application::$ROOT_DIR.'/migrations/'.$migration;

$className = pathinfo($migration, PATHINFO_FILENAME);
var_dump($className);
$instance = new $className();

whenever I'm executing the code I'm getting the following error:

Uncaught Error: Class "m0001_initial" not found in 'my project path'

but whenever I try using the name of class in a direct way like that:

require_once Application::$ROOT_DIR.'/migrations/'.$migration;
$className = pathinfo($migration, PATHINFO_FILENAME);
var_dump($className);
$instance = new m0001_initial();

it works just fine so what could be the problem ?? note that whenever I run var_dump($className); the result is

string(13) "m0001_initial"

Rain
  • 3,416
  • 3
  • 24
  • 40
MHD ANI
  • 11
  • 2
  • How did you load/include the file that actually contains the definition of the `m0001_initial` class? – CBroe Sep 14 '22 at 12:26
  • Not reproducible based on the available info: https://3v4l.org/jQOBe . So I'd agree with the above, possibly the file containing the class isn't included properly - the error seems to be about finding the class you mentioned, not a problem with the name you're generating being wrong. Although admittedly that doesn't really explain why it would work if you hard-code the class name, are you sure nothing else is changed at the same time when you try that? – ADyson Sep 14 '22 at 12:28
  • here's how I loaded the file `require_once Application::$ROOT_DIR.'/migrations/'.$migration;` – MHD ANI Sep 14 '22 at 12:38
  • Well if that failed to load the file it should generate an error. What value does `$migration` have when you run the code? Does that file definitely contain the class? – ADyson Sep 14 '22 at 12:40
  • yes it definitely would contain the class, the value of `$migration` is : `string(17) "m0001_initial.php" ` and it wouldn't be null – MHD ANI Sep 14 '22 at 12:46
  • Still not reproducible on available info: https://3v4l.org/BWt27 (obviously I can't do a real `require_once` call in a sandbox though. There must be something else going on which isn't apparent from what you've shown. – ADyson Sep 14 '22 at 12:50
  • 1
    Are there any namespaces involved in the project? e.g. the class name might actually be `Acme\FooBar\m0001_initial`? You could try `var_dump( m0001_initial::class );` to get its fully qualified name – IMSoP Sep 14 '22 at 15:01
  • @IMSoP I've tried `var_dump( m0001_initial::class );` and the output was `app\migrations\m0001_initial` in m0001_inital file the namespace I specified was `app\migrations` – MHD ANI Sep 14 '22 at 17:32
  • 1
    That's your problem then: namespaces in PHP are only expanded at *compile-time*; to reference a class dynamically at runtime, you need to spell out its fully qualified name, regardless of what namespace and use statements are in effect on that line of code. – IMSoP Sep 14 '22 at 20:09

0 Answers0