我想从一个单独的文件添加功能到类,我不知道这是可能的!从另一个文件分配功能到一个类
$mClass = new MyClass();
$mClass->new_Functions[0](10); // Is there a way to have it in this form?
class myClass
{
private $Pvar = 5;
$new_Fcuntions;
function __construct()
{
include('additional.functions.php');
$arr = get_defined_functions();
$this->new_Functions = $arr['user'];
// trying to call the function with parameter 10
call_user_func(array($this, $this->new_Functions[0]), 10);
}
}
[additional.functions.php]文件
function operate($y)
{
return $this->Pvar * $y;
}
-----编辑-------因为它是不明确! “additional.functions.php”是一个模块,将会有多个模块添加到应用程序中,并且每个模块都可能具有多个功能,并且模块可以互相调用!
additional.functions.php [模块文件]
function operate($y)
{
return $this->Pvar * $y;
}
function do-more($foo)
{
return $this->operate(20) + $foo;
}
another.functions.php [另一模块]
function do-another($foo)
{
return $this->do-more(30) - $foo;
}
function add($foo, $bar)
{
return $foo + $bar;
}
欣赏每一个参与,它已经有一段时间我试图操纵与它在一起! 这是可能的,还是应该放弃!
该函数只能在类内部工作,所以可能需要创建一个函数类,以便其他类可以扩展。 – AbraCadaver 2014-09-30 19:42:43
这是一个非常奇怪的方法,实质上是类继承。我建议你简单地使用包含你的函数的子类来扩展你的当前类。 – 2014-09-30 19:42:58