class MyClass { public $foo; public function __construct($foo) { $this->foo = $foo; } } $reflection = new ReflectionClass('MyClass'); $obj = $reflection->newInstanceArgs(array('bar')); echo $obj->foo; // Output: bar
namespace App\MyPackage; class MyClass { public $foo; public function __construct($foo) { $this->foo = $foo; } } $className = 'App\\MyPackage\\MyClass'; $reflection = new ReflectionClass($className); $obj = $reflection->newInstanceArgs(array('bar')); echo $obj->foo; // Output: barThis example shows how to create a new instance of a class from a namespace using the ReflectionClass. We create a simple class with a constructor that takes an argument inside the "App\MyPackage" namespace. We then use the ReflectionClass to create a new instance of the class with the argument 'bar' and assign it to the $obj variable. Finally, we print out the value of the $foo property of the object to make sure it was initialized correctly. From these examples, we can determine that the ReflectionClass is part of the PHP core package library.