不允许在不同的隔离类中使用相同的函数名?我究竟做错了什么?
我将我的真实代码减少到进行一些测试所需的最低限度。这里是:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
class confFunctions {
function getConf() {
function doWork() {
echo "I am from confFunctions!<br />";
}
doWork();
}
}
class thePage {
function loadPage() {
function doWork() {
echo "I am from thePage!<br />";
}
doWork();
}
}
// Start check.
echo "Checking...<br />";
$conf = new confFunctions();
$conf->getConf();
$page = new thePage();
$page->loadPage();
?>
输出是:
Checking...
I am from confFunctions!
Fatal error: Cannot redeclare doWork() (previously declared in /var/www/Test2/index.php:11) in /var/www/Test2/index.php on line 23
重命名共享名称函数之一可以使一切正常运行。即在第二类中将doWork改为doWork1,如下所示:
class thePage {
function loadPage() {
function doWork1() {
echo "I am from thePage!<br />";
}
doWork1();
}
}
给出正确的结果:
Checking...
I am from confFunctions!
I am from thePage!
如果没有声明为公共,那么类中的内容不应该只对该类可见吗?