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;
}

评论

首页 - Wiki
Copyright © 2011-2024 iteam. Current version is 2.139.0. UTC+08:00, 2024-12-22 17:39
浙ICP备14020137号-1 $访客地图$