having a problem creating an object of class with a given name as a string php

Viewed 52

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"

0 Answers
Related