2017-01-08 11 views
0

개인 변수가 thusly 히 설정할 수 있습니다 반사와 개인 변수에 FCLOSE 호출.

개인 한다거나 할이 thusly 히 액세스 할 수 있습니다 :

은 표면적으로는 일할 수있는이 같은 것 같다

function &getVar($obj, $var) 
{ 
    $reflection = new ReflectionClass(get_class($obj)); 
    $prop = $reflection->getProperty($var); 
    $prop->setAccessible(true); 
    return $prop->getValue($obj); 
} 

fclose(&$getVar($obj, 'fsock')); 

그러나

내 작동하지 않는 것 테스트한다.

아이디어가 있으십니까?

답변

1

&을 제거하면 올바르게 작동합니다.

class Foo{ 
    private $handle; 
} 
$foo = new Foo; 

$handle = fopen(__FILE__,'rb'); 
if($handle){ 
    echo "file opened\n"; 
    setVar($foo, 'handle', $handle); 
    if(fclose(getVar($foo, 'handle'))) echo "file closed\n"; 
} 

출력 :

file opened 
file closed 
다음
function getVar($obj, $var){ 
    $reflection = new ReflectionClass(get_class($obj)); 
    $prop = $reflection->getProperty($var); 
    $prop->setAccessible(true); 
    return $prop->getValue($obj); 
} 

내 테스트 코드입니다