samedi 22 août 2015

Reflection with private __construct

Hi, all!

I trying to make several different objects from Singleton with Reflection on PHP.

I have: $SingletoneObject - variable that contain Singleton object;

$classReflection = new \ReflectionClass(get_class($SingletoneObject));

// get __construct method
$methodReflection = $classReflection->getConstructor();

// make __construct public
$methodReflection->setAccessible(true);

// called method __construct(), so now it is public
$methodReflection->invoke($SingletoneObject, NULL);

$new_object = $classReflection->newInstance();
// return Exception: Access to non-public constructor
// because I changed method to public on methodReflection but not in classReflection

I know that I can make it through method newInstanceWithoutConstructor(). But I want to create it through method newInstance().

Any ideas how do this?





Aucun commentaire:

Enregistrer un commentaire