2011-02-14 19 views
3

今天編寫了一些代碼,發現以下代碼可以在5.3中使用,但不能在早期使用。PHP 5.3中的作用域分辨率和回調差異

<?php 

class Test{ 
    public function uasort(){ 
     $array = array('foo' => 'bar', 123 => 456); 
     uasort($array, 'self::uasort_callback'); 

     return $array; 
    } 

    static private function uasort_callback($a, $b){ 
     return 1; 
    } 
} 

$Test = new Test; 
var_dump($Test->uasort()); 

// version 5.3.2 - works fine 
// version 5.2.13 - Fatal error: Cannot call method self::uasort_callback() or method does not exist 

只是好奇,這是什麼功能調用,無論其認爲好的,壞的(或馬虎)的做法,因爲它更改爲

uasort($array, 'Test::uasort_callback'); 

工作在5.2罰款爲好。

回答

2

根據PHP手冊中關於回調的部分來判斷,我會說它被稱爲「相對靜態類方法調用」。見http://php.net/manual/en/language.pseudo-types.php

// Type 4: Static class method call (As of PHP 5.2.3) 
call_user_func('MyClass::myCallbackMethod'); 

// Type 5: Relative static class method call (As of PHP 5.3.0) 
class A { 
    public static function who() { 
     echo "A\n"; 
    } 
} 

class B extends A { 
    public static function who() { 
     echo "B\n"; 
    } 
} 

call_user_func(array('B', 'parent::who')); // A 

方案稍有不同,但我想打電話給parent::whoself::uasort_callback是在同一個的能力。

+0

很酷,感謝您的鏈接! – postpostmodern 2011-02-15 00:56:41