douju8113
douju8113
2018-03-03 19:44

如何在PHP中的构造函数类中添加方法?

已采纳

I have a class A which needs a new method if one of its arguments is set up to true.

Class A

Class A{

   private $is_new_method;  

   public function __construct( $is_new_method = false, $new_method_name = "" ){

        $this->is_new_method = $is_new_method;

        if( $this->is_new_method ){
           //TODO : add new method in this class based on $new_method_name 
        }      
   }

}

I saw runkit_method_add but it needs (PECL runkit >= 0.7.0).

Notice: this function will be call inside a core of a framework like this :

$foo = new A();
$foo->myNewFunction();

So what is the best way to do this ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongsong1911 dongsong1911 3年前

    Thanks @Jared Farrish ! I found my solution based on your recommandation.

    Class A{
    
        private $is_new_method; 
    
        private $new_method; 
    
        public function __construct( $is_new_method = false, $new_method_name = "" ){
    
            $this->is_new_method = $is_new_method;
    
            if( $this->is_new_method ){
               $new_method = $this->make_my_new_method( $new_method_name ); 
            }      
       }
    
       private function make_my_new_method( $method_name ){
            $functionName  = "foo_" . $method_name;
            $$functionName = function ( $item ) {
                print_r( $item );
            }
            return $$functionName;
       }
    
       public function __call( $method, $args){
           if ( isset( $this->new_method ) ) {
               $func = $this->new_method;
               return call_user_func_array( $func, $args );
           }
       }
    
    }
    
    点赞 评论 复制链接分享
  • dongzhuang2030 dongzhuang2030 3年前
    $a = new Inflector([
        'foo' => function($val){echo sprintf('%s calls foo()', $val[0] ?? 'none!').PHP_EOL;},
        'bar' => function($val){echo sprintf('%s calls bar()', $val[0] ?? 'none!').PHP_EOL;},
        'baz' => function($val){echo sprintf('%s calls baz()', $val[0] ?? 'none!').PHP_EOL;},
    ]);
    
    $a->foo('Ahnold');
    $a->bar('Elvis');
    $a->baz('Lerhman');
    $a->theBreaks('Who');
    
    class Inflector
    {
        private $methods = [];
    
        public function __construct(array $methods)
        {
            $this->methods = $methods;
    
            //var_dump($this->methods);
        }
    
        public function __call(string $methodName, $params)
        {
            if (isset($this->methods[$methodName])) {
                $this->methods[$methodName]($params);
            }
    
            throw new InflectionDeceptionException();
        }
    
    }
    
    class InflectionDeceptionException extends \Exception
    {
        public function __construct($message = "Say what?")
        {
            return parent::__construct($message);
        }
    
    }
    

    https://3v4l.org/kLdOp

    Gives:

    Ahnold calls foo()
    Elvis calls bar()
    Lerhman calls baz()
    
    点赞 评论 复制链接分享

相关推荐