4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
15 * Helpers for bypassing visibility restrictions, mostly used in code generated
16 * by the `sudo` command.
21 * Fetch a property of an object, bypassing visibility restrictions.
23 * @param object $object
24 * @param string $property property name
26 * @return mixed Value of $object->property
28 public static function fetchProperty($object, $property)
30 $refl = new \ReflectionObject($object);
31 $prop = $refl->getProperty($property);
32 $prop->setAccessible(true);
34 return $prop->getValue($object);
38 * Assign the value of a property of an object, bypassing visibility restrictions.
40 * @param object $object
41 * @param string $property property name
44 * @return mixed Value of $object->property
46 public static function assignProperty($object, $property, $value)
48 $refl = new \ReflectionObject($object);
49 $prop = $refl->getProperty($property);
50 $prop->setAccessible(true);
51 $prop->setValue($object, $value);
57 * Call a method on an object, bypassing visibility restrictions.
59 * @param object $object
60 * @param string $method method name
61 * @param mixed $args...
65 public static function callMethod($object, $method, $args = null)
67 $args = func_get_args();
68 $object = array_shift($args);
69 $method = array_shift($args);
71 $refl = new \ReflectionObject($object);
72 $reflMethod = $refl->getMethod($method);
73 $reflMethod->setAccessible(true);
75 return $reflMethod->invokeArgs($object, $args);
79 * Fetch a property of a class, bypassing visibility restrictions.
81 * @param string|object $class class name or instance
82 * @param string $property property name
84 * @return mixed Value of $class::$property
86 public static function fetchStaticProperty($class, $property)
88 $refl = new \ReflectionClass($class);
89 $prop = $refl->getProperty($property);
90 $prop->setAccessible(true);
92 return $prop->getValue();
96 * Assign the value of a static property of a class, bypassing visibility restrictions.
98 * @param string|object $class class name or instance
99 * @param string $property property name
100 * @param mixed $value
102 * @return mixed Value of $class::$property
104 public static function assignStaticProperty($class, $property, $value)
106 $refl = new \ReflectionClass($class);
107 $prop = $refl->getProperty($property);
108 $prop->setAccessible(true);
109 $prop->setValue($value);
115 * Call a static method on a class, bypassing visibility restrictions.
117 * @param string|object $class class name or instance
118 * @param string $method method name
119 * @param mixed $args...
123 public static function callStatic($class, $method, $args = null)
125 $args = func_get_args();
126 $class = array_shift($args);
127 $method = array_shift($args);
129 $refl = new \ReflectionClass($class);
130 $reflMethod = $refl->getMethod($method);
131 $reflMethod->setAccessible(true);
133 return $reflMethod->invokeArgs(null, $args);
137 * Fetch a class constant, bypassing visibility restrictions.
139 * @param string|object $class class name or instance
140 * @param string $const constant name
144 public static function fetchClassConst($class, $const)
146 $refl = new \ReflectionClass($class);
148 return $refl->getConstant($const);