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.1. UTC+08:00, 2024-11-05 20:41
浙ICP备14020137号-1 $bản đồ khách truy cập$