Hướng dẫn php create function dynamically

Okay, challenge accepted!

No matter how weird the question is [it's not btw], let's take it seriously for a moment! It could be useful to have a class that can declare functions and make them real:



Hope someone else finds it useful.

bradyn at NOSPAM dot bradynpoulsen dot com

6 years ago

When trying to make a callable from a function name located in a namespace, you MUST give the fully qualified function name [regardless of the current namespace or use statements].

Daniel Klein

4 years ago

You can use "self::method_name", "static::method_name" and "parent::method_name" in callables:



Results:
Array
[
    [0] => 1: 42
    [1] => 2: 42
    [2] => 3: 42
]
Array
[
    [0] => 1: 42
    [1] => 2: 42
    [2] => 3: 42
]
Array
[
    [0] => 1: 123
    [1] => 2: 123
    [2] => 3: 123
]
Array
[
    [0] => 1: Zaphod Beeblebrox
    [1] => 2: Zaphod Beeblebrox
    [2] => 3: Zaphod Beeblebrox
]

"self::" uses the same class as the called method, "static::" uses the same class as the called class, and "parent::" [not shown] uses the parent class, or generates a warning if there is no parent.

pawel dot tadeusz dot niedzielski at gmail dot com

6 years ago

@edanschwartz at gmail dot com

You can use ::class property to always indicate the class you're in when using static methods:

whysteepy at gmail dot com

5 years ago

Another Appearance of Callbacks! Here is one way of them - methods of an instantiated object can be callable and implemented as variable functions without php's default functions that can call user-defined callback functions.

class Test {
    protected $items = array[];

    public function __construct[]
    {
        $this->items[] = array[$this, 'callBackOne'];
        $this->items[] = array[$this, 'callBackTwo'];
    }

    public function callBackOne[]
    {
        echo __METHOD__ . ' has been called as a callback.';
    }

    public function callBackTwo[]
    {
        echo __METHOD__ . ' has been called as a callback.';
    }

    public function getItems[]
    {
        return $this->items;
    }
}

$o = new Test[];
$itemLists = $o->getItems[];

foreach [$itemLists as $itemList] {

        // call each one as a variable function
        echo '

';
        print_r[$itemList[]];
        echo '
';
}

// Outputs the following
// Test::callBackOne has been called as a callback.

// Test::callBackTwo has been called as a callback.

chechomancr at hotmail dot com

7 months ago

In PHP >= 8.0 callIt function with optional parameters.


How to use:


Result: array [ 'information' => 'Hello world 2022' ]

chris dot rutledge at gmail dot com

3 years ago

Having read this line in the manual above,

"A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1. Accessing protected and private methods from within a class is allowed."

I decided to do some testing to see if I could access private methods using the call_user_func methods. Thankfully not, but for completeness here is my test which also covers using static and object contexts

Dan J

4 years ago

You can avoid repeating a long namespace for classes in callable arrays by making use of the "use" operator and the special "::class" constant.

Documentation of use operator:
//php.net/manual/en/language.namespaces.importing.php

Documentation of ::class constant:
//php.net/manual/en/language.oop5.constants.php

Chủ Đề