Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

Creating PHP class instance from string

// Way #1
$className = "AppMyClass";
$instance = new $className();

// ----------------------

// Way #2
$className = "AppMyClass";
$class = new ReflectionClass($className);

// Create a new Instance without arguments:
$instance = $class->newInstance();

// Create a new Instance with arguments (need a contructor):
$instance = $class->newInstanceArgs(["Banana", "Apple"]);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Creating #PHP #class #instance #string
ADD COMMENT
Topic
Name
9+6 =