[英]Cunstructor invocation failed using ReflectionClass in php
以下代码之前工作正常,但是现在抛出类xyz的错误构造函数调用失败,我添加了有助于理解此问题的代码。
码:
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;
}
构造器类:
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;
}
}
我的PHP版本:5.6
反射创建类的实例,并始终按值传递参数,您遇到的错误是因为您要让构造函数获取指向该参数的指针,但只将其传递一个值。
您可以传递包含如下引用的参数:
function invokeWithReference(StdClass &$class)
{
// Create args array using reference
$args = [&$class];
$obj = new ReflectionClass('xyz');
return $obj->newInstanceArgs($args);
}
您必须从类的构造函数中删除引用,但是将其作为引用参数传递将允许您从类的内部和外部操纵引用:
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'
此处的示例: http : //ideone.com/BNP5Oy
但是 ,这不应该工作,因为参数总是要作为值而不是引用来传递,实际上,将所有引用一起删除仍然可以使此代码起作用:
// 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);
}
这使我相信该实现已被破坏,并且将来可能会被修复,从而可能导致其停止工作。
范例: http : //ideone.com/TkihRs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.