如何在php中使用反射从数组转换为数据结构

如何在php中使用反射从数组转换为数据结构,php,reflection,Php,Reflection,我在php.net上读到,与常规数组相比,SplFixedArray的优势在于它允许更快的数组实现。一些我也想了解的思考。我似乎无法让它工作: $refDLL = new ReflectionClass( 'SplDoublyLinkedList' ); $method = $refDLL->getMethod( 'add' ); $keys = array_keys( $_GET ); $count = count( $keys ); $oIndex = 0; while( $oInde

我在php.net上读到,与常规数组相比,SplFixedArray的优势在于它允许更快的数组实现。一些我也想了解的思考。我似乎无法让它工作:

$refDLL = new ReflectionClass( 'SplDoublyLinkedList' );
$method = $refDLL->getMethod( 'add' );
$keys = array_keys( $_GET );
$count = count( $keys );
$oIndex = 0;
while( $oIndex < $count )
{
    $method( // <-- this seems to be the point of failure
        $oIndex, 
        $_GET[$keys[$oIndex]] 
    );
    $oIndex++;
}
我找到了答案:

$refDLL = new ReflectionMethod( 'SplDoublyLinkedList', 'add' );
$keys = array_keys( $_GET );
$count = count( $keys );
$oIndex = 0;
$sdll = new SplDoublyLinkedList();
while( $oIndex < $count )
{

    $refDLL->invoke( $sdll, 
        $oIndex, 
        $_GET[$keys[$oIndex]] 
    );
    $oIndex++;
}

$sdll->rewind();

while( $sdll->valid() )
{
    print_r( $sdll->key() ); echo '<br />';
    print_r( $sdll->current() ); echo '<br />';
    $sdll->next();
}
输出:

0
pZ0
1
pO1
我找到了答案:

$refDLL = new ReflectionMethod( 'SplDoublyLinkedList', 'add' );
$keys = array_keys( $_GET );
$count = count( $keys );
$oIndex = 0;
$sdll = new SplDoublyLinkedList();
while( $oIndex < $count )
{

    $refDLL->invoke( $sdll, 
        $oIndex, 
        $_GET[$keys[$oIndex]] 
    );
    $oIndex++;
}

$sdll->rewind();

while( $sdll->valid() )
{
    print_r( $sdll->key() ); echo '<br />';
    print_r( $sdll->current() ); echo '<br />';
    $sdll->next();
}
输出:

0
pZ0
1
pO1

这可以做得更容易。Reflection getMethod()不返回闭包,而是
ReflectionMethod
,因此当您使用getMethod()时,可以调用它

 $method = $refDLL->getMethod( 'add' );
 $method->invoke($sdll, $oIndex, $_GET[$keys[$oIndex]] );
出现错误的原因是您试图调用方法,因为它是闭包,但它不是'

编辑:

换衣服

$oIndex = 0;
$sdll = new SplDoublyLinkedList();
while( $oIndex < $count )
{
    $method( // <-- this seems to be the point of failure
        $oIndex, 
        $_GET[$keys[$oIndex]] 
    );
    $oIndex++;
}
$oIndex=0;
$sdll=新的SplDoublyLinkedList();
而($oIndex<$count)
{
$method(//invoke($sdll,$oIndex,$\u GET[$keys[$oIndex]]);
}

顺便说一句,使用while循环可以很容易地替换为for循环。

这样做更容易。Reflection getMethod()不返回闭包,而是返回ReflectionMethod,因此当您使用getMethod()时,可以调用它

 $method = $refDLL->getMethod( 'add' );
 $method->invoke($sdll, $oIndex, $_GET[$keys[$oIndex]] );
出现错误的原因是您试图调用方法,因为它是闭包,但它不是'

编辑:

换衣服

$oIndex = 0;
$sdll = new SplDoublyLinkedList();
while( $oIndex < $count )
{
    $method( // <-- this seems to be the point of failure
        $oIndex, 
        $_GET[$keys[$oIndex]] 
    );
    $oIndex++;
}
$oIndex=0;
$sdll=新的SplDoublyLinkedList();
而($oIndex<$count)
{
$method(//invoke($sdll,$oIndex,$\u GET[$keys[$oIndex]]);
}

顺便说一句,您使用while循环的方式很容易被for循环所取代。

您遇到的错误消息是什么?该代码为我生成了一个不同的错误-
未捕获异常“ReflectionException”和消息“Method add not existence”
您遇到的错误消息是什么?该代码生成的我遇到了另一个错误-
未捕获异常“ReflectionException”,消息为“Method add不存在”
这一点非常不清楚。能否显示“getMethod”和“invoke”的工作示例?您也是对的。$refDLL=new ReflectionClass('SplDoublyLinkedList');$Method=$refDLL->getMethod('add');$keys=array\u keys($\u GET);$count=count($keys);$oIndex=0;$sdll=new SplDoublyLinkedList();while($oIndex<$count){$method->invoke($sdll,$oIndex,$_GET[$keys[$oIndex]]);$oIndex++}什么不清楚?将闭包更改为调用对象上的方法?$refDLL是一个对象,$sdll是另一个对象。我花了一分钟才弄清楚它们是同一个对象。这非常不清楚。您能展示一个“getMethod”和“invoke”的工作示例吗?您也是对的。$refDLL=new ReflectionClass('SplDoublyLinkedList'));$method=$refDLL->getMethod('add');$keys=array_keys($\u GET);$count=count($keys);$oIndex=0;$sdll=new SplDoublyLinkedList();while($oIndex<$count){$method->invoke($sdll,$oIndex,$\u GET[$keys[$oIndex]]);$oIndex++}还不清楚什么?将闭包更改为调用对象上的方法?$refDLL是一个对象,$sdll是另一个对象。我花了一分钟才弄清楚它们是同一个对象。