How to add methods dynamically

后端 未结 8 1345
故里飘歌
故里飘歌 2021-01-01 15:15

I\'m trying to add methods dynamically from external files. Right now I have __call method in my class so when i call the method I want, __call inc

相关标签:
8条回答
  • 2021-01-01 15:53

    You can dynamically add attributes and methods providing it is done through the constructor in the same way you can pass a function as argument of another function.

    class Example {
        function __construct($f)
        {
           $this->action=$f;
        }
    }
    
    function fun() {
       echo "hello\n";
    }
    
    $ex1 = new class('fun');
    

    You can not call directlry $ex1->action(), it must be assigned to a variable and then you can call this variable like a function.

    0 讨论(0)
  • 2021-01-01 15:54

    Is this what you need?

    $methodOne = function ()
    {
        echo "I am  doing one.".PHP_EOL;
    };
    
    $methodTwo = function ()
    {
        echo "I am doing two.".PHP_EOL;
    };
    
    class Composite
    {
        function addMethod($name, $method)
        {
            $this->{$name} = $method;
        }
    
        public function __call($name, $arguments)
        {
            return call_user_func($this->{$name}, $arguments);
        }
    }
    
    
    $one = new Composite();
    $one -> addMethod("method1", $methodOne);
    $one -> method1();
    $one -> addMethod("method2", $methodTwo);
    $one -> method2();
    
    0 讨论(0)
  • 2021-01-01 15:56

    I've worked up the following code example and a helper method which works with __call which may prove useful. https://github.com/permanenttourist/helpers/tree/master/PHP/php_append_methods

    0 讨论(0)
  • 2021-01-01 15:59

    if i read the manual right, the __call get called insted of the function, if the function dosn't exist so you probely need to call it after you created it

    Class myClass
    {
        function __call($name, $args)
        {
            require_once($name.".php");
            $this->$name($args);
        }
    }
    
    0 讨论(0)
  • 2021-01-01 15:59

    You can create an attribute in your class : methods=[]
    and use create_function for create lambda function.
    Stock it in the methods attribute, at index of the name of method you want.
    use :

    function __call($method, $arguments)
    {
       if(method_exists($this, $method))
          $this->$method($arguments);
       else
          $this->methods[$method]($arguments);
    }
    

    to find and call good method.

    0 讨论(0)
  • 2021-01-01 16:07
    /**
     * @method Talk hello(string $name)
     * @method Talk goodbye(string $name)
     */
    class Talk {
        private $methods = [];
        
        public function __construct(array $methods) {
            $this->methods = $methods;
        }
        
        public function __call(string $method, array $arguments): Talk {
            if ($func = $this->methods[$method] ?? false) {
                $func(...$arguments);
                
                return $this;
            }
            
            throw new \RuntimeException(sprintf('Missing %s method.'));
        }
    }
    
    $howdy = new Talk([
        'hello' => function(string $name) {
            echo sprintf('Hello %s!%s', $name, PHP_EOL);
        },
        'goodbye' => function(string $name) {
            echo sprintf('Goodbye %s!%s', $name, PHP_EOL);
        },
    ]);
    
    $howdy
        ->hello('Jim')
        ->goodbye('Joe');
    

    https://3v4l.org/iIhph

    0 讨论(0)
提交回复
热议问题