开发者

PHP add methods to Functions

开发者 https://www.devze.com 2023-03-14 11:00 出处:网络
Is it possible to add methods to functions? For example: <? function func(){ ; } //add method func->test = function(){

Is it possible to add methods to functions?

For example:

<?
function func(){
    ;
}
//add method
func->test = function(){
    ;
}

func->test();
func();

I'm coming from a javascript background, and therefore I'm used to 'everything is an object'.

EDIT:

I was just explaining where the misconception may often come from for new phpers. I understand the above code doesn't work.

EDIT 2

Figured it out.

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&开发者_开发问答gt;test<br>';
};

$func->test();
$func();

Even sexier :)

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();


No.

Not everything is an object in PHP. In fact the only thing that is an object is, well, an object. More specifically, and generally, an instantiation of a class.

Your code converted to PHP

// function_object.php
<?php

class FunctionObject {

   public method func() {
       // do stuff

   }

}

?>

In other code you would use it like this:

<?php

// example.php in same folder as function_object.php
include 'function_object.php';

$FuncObj = new FunctionObject;
$FuncObj->func();

Also: read more about PHP & OOP


No, because an object is a different PHP language construct than a function. Functions do not have properties, but are instead simply execution instructions.

But, if func were instead a pre-defined class, then yes... with a bit of witchcraft, ignoring public outcry, foregoing readability and PHP coding standards, and by using closures with the __call() magic 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);

This won't work as you'd think without __call(), because by $obj->test(1,1), PHP thinks you're trying to call a non-existent method of func when out of object scope. But inside, being that the new "test" property is of a type: closure, the call_user_func_array() just sees the "test" property as just another function, so you can hide this bit of trickery from outside scope.


You would need your function func() to return an object, then you'd be able to do something like: func()->test();

But please note that your way of handling objects is not right in PHP and I suggest that you go read the OO documentations here.


In difference to javacript, in PHP not everything is an object. Therefore you need to differ between function and class.

If you want to create an object, you need to define the class first.

class myClass {
}

You can then add as many functions to the class as you need. But you need to define them first:

class myClass {
  function test() {
    echo "test!\n";
  }
}

When everything is ready, you can bring it to life then:

$class = new myClass;
$class->test();

Checkout the manual for more.


You can't do what you're trying to do, but you can define functions inside of other functions.

This example outputs text:

function a() {
    function b() { echo 'Hi'; }
}
a();
b();
Output: HiHi

This example outputs an error:

function a() {
    function b() { echo 'Hi'; }
}
b();
Output: ERROR
0

精彩评论

暂无评论...
验证码 换一张
取 消