php - How to use variable name to call a class? -
i want use variable (string value) call class. can ? search php reflectionclass not know how use method reflection result. this:
foreach($menutypes $key => $type){ if($key != 'link'){ $class = new \reflectionclass('\app\models\\' . $key); //now $class reflectionclass object //example: $key "product" //i'm fail here , cannot call method get() of //the class product $data[strtolower($key) . '._items'] = $class->get(); } }
without reflectionclass:
$instance = new $classname();
with reflectionclass: use reflectionclass::newinstance()
method:
$instance = (new \reflectionclass($classname))->newinstance();
Comments
Post a Comment