7
이것이 작동하지 않는 것 같습니다. 그 이유는 무엇입니까? 정적이 아닌 메서드 내에서 정적 클로저를 만들 수 있습니다. 그 반대의 이유는 무엇입니까?정적 함수 내부에서 생성 된 클로저를 인스턴스에 바인딩하는 방법
class RegularClass {
private $name = 'REGULAR';
}
class StaticFunctions {
public static function doStuff()
{
$func = function()
{
// this is a static function unfortunately
// try to access properties of bound instance
echo $this->name;
};
$rc = new RegularClass();
$bfunc = Closure::bind($func, $rc, 'RegularClass');
$bfunc();
}
}
StaticFunctions::doStuff();
// PHP Warning: Cannot bind an instance to a static closure in /home/codexp/test.php on line 19
// PHP Fatal error: Using $this when not in object context in /home/codexp/test.php on line 14
흠이 ['bindTo'] 작동하지 않는 것 (http://www.php.net/manual/en/closure : "Static closures cannot have any bound object (the value of the parameter newthis should be NULL), but this function can nevertheless be used to change their class scope." 난 당신이 뭔가를해야 할 것이다 생각 .bindto.php). – Charles
bindTo docummentation에서 "정적 클로저는 바인딩 된 객체를 가질 수 없습니다 (매개 변수 new의 값은 NULL이어야 함). 그러나이 함수는 클래스 범위를 변경하는 데에도 사용할 수 있습니다." http://www.php.net/manual/en/closure.bindto.php 그리고 bind는 단지 bindTo의 정적이기 때문에 정적 컨텍스트에서 정적이 아닌 클로저를 만들 수는 없다고 생각합니다. – FabioCosta
이것은 PHP 7에서 수정되었습니다. 버그 [# 64761] (https://bugs.php.net/bug.php?id=64761) 및 [# 68792] (https://bugs.php.net/bug)를 참조하십시오. .php? id = 68792). – bishop