我在 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++;
}
错误:
PHP Fatal error: Uncaught Error: Function name must be a string in
C:'inetpub'wwwroot'objstr.php:26
Stack trace:
#0 {main}
thrown in C:'inetpub'wwwroot'objstr.php on line 26
我找到了答案:
$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();
}
查询:
?zero=pZ0&one=pO1
输出:
0
pZ0
1
pO1
它可以更容易地完成。反射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++;
}
自
$sdll = new SplDoublyLinkedList();
for ($oIndex = 0; $oIndex < $count; ++$oIndex )
{
$method->invoke($sdll, $oIndex, $_GET[$keys[$oIndex]] );
}
顺便说一句,您使用 while 循环的方式可以很容易地替换为 for 循环。