草庐IT

distanceFunc

全部标签

php - 在 PHP 中保存函数的类变量

PHP允许变量像这样保存函数:$f=function($a,$b){print"$a$b";};$f("Hello","World!");//prints'HelloWorld!'这对我来说很好用。我试图将一个函数传递给一个类并设置一个实例变量来保存该函数,但运气不佳:classClusterer{private$distanceFunc;publicfunction__construct($f){$this->distanceFunc=$f;print$f(1,7);//worksprint$this->distanceFunc(1,7);//exceptionsanderrorsa