doutuo7815 2017-05-15 18:34
浏览 14
已采纳

在课堂上添加常规非反义函数

I wanted to add functions to class, those functions are in separate file(s), those functions contain ($this).

class myClass {
    private $myFunctions=array(); // will contain two keys (title, function_object)
    private $var;

    public function __construct() {
        $this->var = 'Hello world';
    }

    public function add_function($title, $func) {
        $this->myFunctions[$title] = $func;
    }

}

function add($x, $y)    {
    echo $this->var; 
    return $x + $y; 
}

$class = new myClass;
$class->add_function('add', 'add()');

echo $class->add(1,2);

my goal is to add regular functions (not anonymous functions) to the class or to assign those function(s) to a var which can be passed to the class!

Can this be achieved ?

  • 写回答

3条回答 默认 最新

  • dqf42223 2017-05-15 19:04
    关注

    You must have to understand the oops concepts here.

    $this refers to your current class / object. So all functions inside a class will be able to access $this ( Current Object ).

    But the function declared out side of class can not use $this of that object. (Are you clear up to here.)

    But anyways nothing is impossible.

    You can achieve your goal in at-least 2 ways.

    2) You can use trait to implement multiple inheritance.

    it will be like this

    trait commonFunctions
    {
    
        function add($a, $b)
        {
            $this->result = $a + $b;
    
            return $this;
        }
    }
    
    class myClass
    {
    
        use commonFunctions;
    
        public function myClassFunction()
        {
            // 
        }
    }
    

    2 ) Or you can follow below code. Source php.net

    <?php
    
    class A {
        function __construct($val) {
            $this->val = $val;
        }
        function getClosure() {
            //returns closure bound to this object and scope
            return function() { return $this->val; };
        }
    }
    
    $ob1 = new A(1);
    $ob2 = new A(2);
    
    $cl = $ob1->getClosure();
    echo $cl(), "
    ";
    $cl = $cl->bindTo($ob2);
    echo $cl(), "
    ";
    

    Here __call will be called when undeclared method is being called. So you can dynamically append methods to class by this way.

    Of course you can polyfill your object in javascript like way. But keep in mind you need to use Reflection to bind this to closure.

    <?php
    
    class A {
    
        private $dynamic_functions=[];
    
        public function __call($method, $args=[])
        {
            if(isset($this->dynamic_functions[$method]) && $this->dynamic_functions[$method] instanceof \Closure)
            {
                $cl = $this->dynamic_functions[$method]->bindTo($this);
    
                return call_user_func_array($cl, $args);
            }
            else
            {
                die("Wrong method, Not yet polyfilled.");
            }
        }
    
        public function __set($property, $value)
        {
            $this->dynamic_functions[$property] = $value;
        }
    }
    
    $a = new A;
    
    $a->sum = function($a,$b){
    
        // var_dump($this); will give you current instance of A
    
        return $a+$b;
    };
    
    echo $a->sum(5, 3);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧