PHP访问类私有属性
来源:互联网 发布:阿里云ecs手动搭建 编辑:程序博客网 时间:2024/06/06 13:57
除了常用的 __get 方法外,记录一个反射API的方法。
/** * set class's public/private/protected property * * @param object $class * @param string $variant property name * @param string $value value * * @return array */if (!function_exists('set_property')){function set_property($class, $variant, $value){ if (!is_object($class)) throw new Exception('paramater #0 must be an object\'s instance.', 1); $property = (new ReflectionClass($class))->getProperty($variant); $property->setAccessible(true); return $property->setValue($class, $value);}}/** * get class's public/private/protected property * * @param object $class * @param string $variant property name * * @return array */if (!function_exists('get_property')){function get_property($class, $variant){ if (!is_object($class)) throw new Exception('paramater #0 must be an object\'s instance.', 1); $property = (new ReflectionClass($class))->getProperty($variant); $property->setAccessible(true); return $property->getValue($class);}}/** * call class's public/private/protected method * * @param object $class * @param string $variant property name * @param string $value value * * @return array */if (!function_exists('call_class_method_array')){function call_class_method_array($class, $method, $parameters){ if (!is_object($class)) throw new Exception('paramater #0 must be an object\'s instance.', 1); $reflectionMethod = (new ReflectionClass($class))->getMethod($method); $reflectionMethod->setAccessible(true); return $reflectionMethod->invokeArgs($class, $parameters);}}/** * call class's public/private/protected method * * @param object $class * @param string $variant property name * @param string $value value * * @return array */if (!function_exists('call_class_method')){function call_class_method($class, $method, ...$parameters){ if (!is_object($class)) throw new Exception('paramater #0 must be an object\'s instance.', 1); $reflectionMethod = (new ReflectionClass($class))->getMethod($method); $reflectionMethod->setAccessible(true); return $reflectionMethod->invokeArgs($class, $parameters);}}
===========实例代码:
$a = new \ReflectionClass(CouponStatistics::class); $property = $a->getProperty('fillable'); $property->setAccessible(true); $class = $property->getValue($a->newInstance());
0 1
- PHP访问类私有属性
- php中类外部访问类私有属性的方法
- 反射访问类的私有属性对象的私有方法
- Python访问私有属性
- PHP面向对象----(私有属性的访问方法)
- php在对象之外访问其私有属性private及保护属性protected的特例
- 通过反射访问任意类的私有方法和属性
- 利用反射机制访问父类私有变量和属性
- iOS中访问和修改一个类的私有属性
- 使用Java反射机制访问类的私有属性
- 访问或修改一个类的私有属性
- 通过java反射技术访问类中私有成员属性
- JAVA在子类中访问父类的私有属性
- python 直接访问类的私有属性或方法
- 子类会继承父类的私有方法和私有属性,只是不能直接访问
- Python访问类的私有属性和私有方法的正确姿势
- php对象的私有属性权限限制在类
- 访问私有类成员
- spring-boot-mybatis-demo
- centos 安装nginx
- 【图像处理】ICC_Profile Tags
- ubuntu常见操作备注
- json数据
- PHP访问类私有属性
- 使用 visual studio 调试android NDK 程序
- spring applicationContext.xml 配置文件 详解
- android操作sdcard中的多媒体文件(一)——音乐列表的制作
- MarkdownPad2.5 注册码
- 使用moy快速开发后台管理系统(四)
- 【图像处理】PrintIM Tags
- 51Nod-1737-配对
- 机器人操作系统ROS Indigo 入门学习(8)——理解ROS service 和parameter