Following code was working earlier but now it's throwing error constructor invocation of class xyz failed, I have added the code that will help to understand the issue.
code:
public static function & Instance( $class )
{
static $loaded = array();
if ( !( isset( $loaded[ $class ] ) ) ) {
$c = SPLoader::loadClass( $class, false, null, false );
if ( !( strlen( $c ) ) ) {
$c = SPLoader::loadClass( $class, defined( 'SOBIPRO_ADM' ) );
}
if ( !( strlen( $c ) ) ) {
throw new SPException( SPLang::e( 'Cannot create instance of "%s". Class file does not exist', $class ) );
}
$loaded[ $class ] = $c;
}
$args = func_get_args();
unset( $args[ 0 ] );
try {
$obj = new ReflectionClass( $loaded[ $class ] );
$instance = $obj->newInstanceArgs( $args );
} catch ( LogicException $Exception ) {
throw new SPException( SPLang::e( 'Cannot create instance of "%s". Class file does not exist. Error %s', $class, $Exception->getMessage() ) );
} catch ( ReflectionException $Exception ) {
throw new SPException( SPLang::e( 'Cannot create instance of "%s". Class file does not exist. Error %s', $class, $Exception->getMessage() ) );
}
return $instance;
}
Constructor class:
class SPImexExportDownload
{
/**
* @var SPImexCtrl
*/
protected $proxy = null;
public function __construct( SPImexCtrl &$proxy )
{
$this->proxy =& $proxy;
}
public function data( $field )
{
$data = $field->getRaw();
$out = array();
try {
$data = SPConfig::unserialize( $data );
if ( count( $data ) ) {
// "{'label':'Nothing Special','protocol':'http','url':'radek.suski.eu'}"
if ( isset( $data[ 'label' ] ) && $data[ 'label' ] ) {
$out[ ] = $data[ 'label' ];
}
$out[ ] = $data[ 'protocol' ] . '://' . $data[ 'url' ];
}
}
catch ( SPException $x ) {
$this->proxy->log( $field->get( 'nid' ) . ": " . $x->getMessage(), 'error' );
$data = null;
}
return $out;
}
}
My PHP Version: 5.6
Reflection creates an instance of the class and always passes arguments by value, the error you're experiencing is because you're asking the constructor to get a pointer to the argument but it's only being passed a value.
You can pass arguments which contain references like this:
function invokeWithReference(StdClass &$class)
{
// Create args array using reference
$args = [&$class];
$obj = new ReflectionClass('xyz');
return $obj->newInstanceArgs($args);
}
You'll have to remove the reference from the constructor in your class, but passing it as a reference argument will allow you to manipulate the reference from inside and outside the class:
class xyz
{
public $class;
public function __construct(StdClass $class)
{
// May as well remove the reference here since it'll only be
// a reference to the value passed
$this->class = $class;
}
public function setTest2()
{
$this->class->test2 = 'goodbye';
}
}
$class = new StdClass;
$instance = invokeWithReference($class);
$class->test = 'hello';
$instance->setTest2();
echo $instance->class->test; // Echos 'hello'
echo PHP_EOL;
echo $class->test2; // Echos 'goodbye'
Example here: http://ideone.com/BNP5Oy
However , this shouldn't work as the arguments are always meant to be passed as a value and not a reference, in fact removing references all together still allowed this code to work:
// Still works
function invokeWithReference(StdClass $class)
{
// Create args array using reference
$args = [$class];
$obj = new ReflectionClass('xyz');
return $obj->newInstanceArgs($args);
}
// Also still works
function invokeWithReference()
{
$args = func_get_args();
$obj = new ReflectionClass('xyz');
return $obj->newInstanceArgs($args);
}
This leads me to believe the implementation is broken and could be fixed in the future which could cause this to stop working.
Examples: http://ideone.com/TkihRs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.