Here是一个简化我的代码:为什么进入父方法的函数会被声明两次?
<?php
class questions {
public function index($from = null) {
if ($from != 'test') {
return $this->test();
}
return 'sth';
}
public function test(){
function myfunc(){}
return $this->index(__FUNCTION__);
}
}
class tags extends questions {
public function index() {
return parent::index();
}
}
$obj = new tags;
echo $obj->index();
正如您在拨弄看到,它抛出这个错误:
Warning: Declaration of tags::index() should be compatible with questions::index($from = NULL) in /in/Y5KVq on line 29
Fatal error: Cannot redeclare myfunc() (previously declared in /in/Y5KVq:16) in /in/Y5KVq on line 16
Process exited with code 255
为什么?自然myfunc()
应该声明一次。由于test()
将被调用一次。那么错误说的是什么?
无论如何,我该如何解决它?
。如果您要在可能运行多次的代码中动态声明它,您应该将其包装在'if(!function_exists('myfunc'))'检查中。 – rickdenhaan
@rickdenhaan是的,我可以做你检查你提到的。但是我想知道为什么这个函数会被执行多次?代码将如何编译? –