Objects.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Common Object Functions
  4. *
  5. * PHP version 5
  6. *
  7. * @category Common
  8. * @package Functions\Objects
  9. * @author Jim Wigginton <terrafrost@php.net>
  10. * @copyright 2016 Jim Wigginton
  11. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  12. * @link http://phpseclib.sourceforge.net
  13. */
  14. namespace phpseclib\Common\Functions;
  15. /**
  16. * Common Object Functions
  17. *
  18. * @package Functions\Objects
  19. * @author Jim Wigginton <terrafrost@php.net>
  20. */
  21. abstract class Objects
  22. {
  23. /**
  24. * Accesses a private variable from an object
  25. *
  26. * @param Object $obj
  27. * @param string $var
  28. * @return mixed
  29. * @access public
  30. */
  31. public static function getVar($obj, $var)
  32. {
  33. $reflection = new \ReflectionClass(get_class($obj));
  34. $prop = $reflection->getProperty($var);
  35. $prop->setAccessible(true);
  36. return $prop->getValue($obj);
  37. }
  38. /**
  39. * Sets the value of a private variable in an object
  40. *
  41. * @param Object $obj
  42. * @param string $var
  43. * @param mixed $val
  44. * @return mixed
  45. * @access public
  46. */
  47. public static function setVar($obj, $var, $val)
  48. {
  49. $reflection = new \ReflectionClass(get_class($obj));
  50. $prop = $reflection->getProperty($var);
  51. $prop->setAccessible(true);
  52. return $prop->setValue($obj, $val);
  53. }
  54. /**
  55. * Accesses a private method from an object
  56. *
  57. * @param Object $obj
  58. * @param string $func
  59. * @param array $params
  60. * @return mixed
  61. * @access public
  62. */
  63. public static function callFunc($obj, $func, $params = array())
  64. {
  65. $reflection = new \ReflectionClass(get_class($obj));
  66. $method = $reflection->getMethod($func);
  67. $method->setAccessible(true);
  68. return $method->invokeArgs($obj, $params);
  69. }
  70. }