Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PHP add methods to Functions
- <?
- function func(){
- ;
- }
- //add method
- func->test = function(){
- ;
- }
- func->test();
- func();
- class myfunc_class{
- function __invoke(){
- //function body
- }
- function __call($closure, $args)
- {
- call_user_func_array($this->$closure, $args);
- }
- }
- $func = new myfunc_class;
- $func->test = function(){
- echo '<br>test<br>';
- };
- $func->test();
- $func();
- class func{
- public $_function;
- function __invoke(){
- return call_user_func_array($this->_function,func_get_args());
- }
- function __construct($fun){
- $this->_function = $fun;
- }
- function __call($closure, $args)
- {
- call_user_func_array($this->$closure, $args);
- }
- }
- $func = new func(function($value){
- echo $value;
- });
- $func->method = function(){
- echo '<br>test<br>';
- };
- $func('someValue');
- $func->method();
- class func
- {
- function __call($func, $args)
- {
- return call_user_func_array($this->$func, $args);
- }
- }
- $obj = new func;
- $obj->test = function($param1, $param2)
- {
- return $param1 + $param2;
- };
- echo $obj->test(1,1);
- // function_object.php
- <?php
- class FunctionObject {
- public method func() {
- // do stuff
- }
- }
- ?>
- <?php
- // example.php in same folder as function_object.php
- include 'function_object.php';
- $FuncObj = new FunctionObject;
- $FuncObj->func();
- class myClass {
- }
- class myClass {
- function test() {
- echo "test!n";
- }
- }
- $class = new myClass;
- $class->test();
- function a() {
- function b() { echo 'Hi'; }
- }
- a();
- b();
- Output: HiHi
- function a() {
- function b() { echo 'Hi'; }
- }
- b();
- Output: ERROR
Advertisement
Add Comment
Please, Sign In to add comment