xiaozi 2015-01-10 20:35:29

php < 5.3.2中没有提供ReflectionMethod::setAccessible,那怎么获取私有属性呢?@qiyuuu

function getPrivatePropertyOfObj($obj, $name) {
    $arr = (array)$obj;
    $key = "\0" . get_class($obj) . "\0" . $name;
    return isset($arr[$key]) ? $arr[$key] : null;
}

function getProtectedPropertyOfObj($obj, $name) {
    $arr = (array)$obj;
    $key = "\0*\0" . $name;
    return isset($arr[$key]) ? $arr[$key] : null;
}

评论

trang chủ - Wiki
Copyright © 2011-2024 iteam. Current version is 2.137.3. UTC+08:00, 2024-11-28 13:29
浙ICP备14020137号-1 $bản đồ khách truy cập$