Guest User

Untitled

a guest
Jun 11th, 2012
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. PHP add methods to Functions
  2. <?
  3. function func(){
  4. ;
  5. }
  6. //add method
  7. func->test = function(){
  8. ;
  9. }
  10.  
  11. func->test();
  12. func();
  13.  
  14. class myfunc_class{
  15. function __invoke(){
  16. //function body
  17. }
  18. function __call($closure, $args)
  19. {
  20. call_user_func_array($this->$closure, $args);
  21. }
  22. }
  23. $func = new myfunc_class;
  24.  
  25. $func->test = function(){
  26. echo '<br>test<br>';
  27. };
  28.  
  29. $func->test();
  30. $func();
  31.  
  32. class func{
  33. public $_function;
  34. function __invoke(){
  35. return call_user_func_array($this->_function,func_get_args());
  36. }
  37. function __construct($fun){
  38. $this->_function = $fun;
  39. }
  40. function __call($closure, $args)
  41. {
  42. call_user_func_array($this->$closure, $args);
  43. }
  44. }
  45. $func = new func(function($value){
  46. echo $value;
  47. });
  48.  
  49. $func->method = function(){
  50. echo '<br>test<br>';
  51. };
  52.  
  53. $func('someValue');
  54. $func->method();
  55.  
  56. class func
  57. {
  58.  
  59. function __call($func, $args)
  60. {
  61. return call_user_func_array($this->$func, $args);
  62. }
  63.  
  64. }
  65.  
  66.  
  67. $obj = new func;
  68.  
  69. $obj->test = function($param1, $param2)
  70. {
  71. return $param1 + $param2;
  72. };
  73.  
  74. echo $obj->test(1,1);
  75.  
  76. // function_object.php
  77. <?php
  78.  
  79. class FunctionObject {
  80.  
  81. public method func() {
  82. // do stuff
  83.  
  84. }
  85.  
  86. }
  87.  
  88. ?>
  89.  
  90. <?php
  91.  
  92. // example.php in same folder as function_object.php
  93. include 'function_object.php';
  94.  
  95. $FuncObj = new FunctionObject;
  96. $FuncObj->func();
  97.  
  98. class myClass {
  99. }
  100.  
  101. class myClass {
  102. function test() {
  103. echo "test!n";
  104. }
  105. }
  106.  
  107. $class = new myClass;
  108. $class->test();
  109.  
  110. function a() {
  111. function b() { echo 'Hi'; }
  112. }
  113. a();
  114. b();
  115. Output: HiHi
  116.  
  117. function a() {
  118. function b() { echo 'Hi'; }
  119. }
  120. b();
  121. Output: ERROR
Advertisement
Add Comment
Please, Sign In to add comment