Php 功能存在
好的,我有一个小MVC,它的工作原理是什么 somesite/classname/classfunction/functionPhp 功能存在,php,oop,Php,Oop,好的,我有一个小MVC,它的工作原理是什么 somesite/classname/classfunction/function class test(){ public function test2(){ // action will be 'function' in adress $action = $this->action ? $this->action : array($this, 'test3'); function test3(){
class test(){
public function test2(){
// action will be 'function' in adress
$action = $this->action ? $this->action : array($this, 'test3');
function test3(){
print 1;
}
$action();
}
}
因此,如果我们运行somesite/test/test2/test3,它将打印“1”,但如果我们运行somesite/test/test2/phpinfo,它将显示phpinfo。
问题:如何检查类函数中函数的存在性
UPD
不要忘记phpinfo,函数_exists将显示它。
方法_存在于类函数中,但不存在于类函数的函数中
UPD解决方案
你也应该看看神奇的方法
您还应该检查magic方法要检查类中是否存在某个方法,请使用: 您还可以使用类名:
if (method_exists("SomeClass", "someMethod")) {
$c = new SomeClass();
$c->someMethod();
}
要解决您的问题,请将test3设为类方法:
class test(){
private function test3() {
print 1;
}
public function test2(){
// action will be 'function' in adress
$action = $this->action ? $this->action : array($this, 'test3');
if (method_exists($this, $action)) {
$this->$action();
} else {
echo "Hey, you cannot call that!";
}
}
}
要检查类中是否存在某个方法,请使用: 您还可以使用类名:
if (method_exists("SomeClass", "someMethod")) {
$c = new SomeClass();
$c->someMethod();
}
要解决您的问题,请将test3设为类方法:
class test(){
private function test3() {
print 1;
}
public function test2(){
// action will be 'function' in adress
$action = $this->action ? $this->action : array($this, 'test3');
if (method_exists($this, $action)) {
$this->$action();
} else {
echo "Hey, you cannot call that!";
}
}
}
类中的函数称为方法。因此,在类内部,简单地说,如果方法_存在$this,'methodName',则test2中的test3不能使用数组$this,'test3'调用。您应该避免函数中的函数。希望这只是一个糟糕的复制粘贴示例。@deceze通常避免函数中的函数,或者在php中避免函数?@metal-php。在其他语言中,函数中的函数是一种核心设计模式。注意,我也不是指匿名函数。我指的是在另一个函数中的裸露的、全局的、有条件的函数声明。给我定义的函数。很好地分组到系统和用户。这是清除phpinfo的一种方法。尽管如此,我还是会使用方法_exists,并将test3作为类方法,使其干净,类中的OOP.Functions称为METHODS。因此,在类内部,简单地说,如果方法_存在$this,'methodName',则test2中的test3不能使用数组$this,'test3'调用。您应该避免函数中的函数。希望这只是一个糟糕的复制粘贴示例。@deceze通常避免函数中的函数,或者在php中避免函数?@metal-php。在其他语言中,函数中的函数是一种核心设计模式。注意,我也不是指匿名函数。我指的是在另一个函数中的裸露的、全局的、有条件的函数声明。给我定义的函数。很好地分组到系统和用户。这是清除phpinfo的一种方法。尽管如此,我还是会使用方法_exists并使test3成为一个类方法,以使其成为干净的OOP。我不需要使用test3作为类的方法,只作为类方法的函数。bcz如果它是类方法,它将作为函数,并且可以从MVC结构(如/test)调用/test3@Abyss啊。我明白了。也许你应该在问题中解释一下。@Abysis:如果你声明它是私有的,那也是吗?我也正准备这么做,但是我的PHP安装没有匿名函数,所以我的测试床无法工作;-。如果您愿意,您可以回答自己的问题。我不需要将test3用作类的方法,只需要用作类方法的函数。bcz如果它是类方法,它将作为函数,并且可以从MVC结构(如/test)中调用/test3@Abyss啊。我明白了。也许你应该在问题中解释一下。@Abysis:如果你声明它是私有的,那也是吗?我也正准备这么做,但是我的PHP安装没有匿名函数,所以我的测试床无法工作;-。如果愿意,您可以回答自己的问题。请注意,PHP5.3.0支持匿名函数。请注意,PHP5.3.0支持匿名函数。
class test(){
private function test3() {
print 1;
}
public function test2(){
// action will be 'function' in adress
$action = $this->action ? $this->action : array($this, 'test3');
if (method_exists($this, $action)) {
$this->$action();
} else {
echo "Hey, you cannot call that!";
}
}
}
class test{
public function test2(){
// site/test/test2/test3
$tmpAction = $this->parenter->actions[1]; // test3
$test3 = function(){
print 1;
};
if(isset($$tmpAction)){
$$tmpAction();
}else{
$this->someDafaultFunc();
}
}
}