Answer the question
In order to leave comments, you need to log in
Is it possible to create a class object in php if the class name is given through a string?
Let's say we have a code like this:
<?php
class Audi{}
class Ford{}
function getCar($name){
//...
}
$audi = getCar('Audi');
Answer the question
In order to leave comments, you need to log in
function getCar($className){
return new $className();
}
demo Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question