初学PHP,以前做vc的,从vc的角度感觉很难接受,求助!
class SC_Utils_Ex extends SC_Utils {
}class SC_Utils { // インストール初期処理
function sfInitInstall() {
// インストール済みが定義されていない。
if (!defined('ECCUBE_INSTALL')) {
$phpself = $_SERVER['PHP_SELF'];
if( !ereg('/install/', $phpself) ) {
$path = substr($phpself, 0, strpos($phpself, basename($phpself)));
$install_url = SC_Utils_Ex::class SC_Utils_Ex($path);
header('Location: ' . $install_url);
exit;
}
}
$path = HTML_REALDIR . 'install/' . DIR_INDEX_FILE;
if(file_exists($path)) {
SC_Utils_Ex::sfErrorHeader('>> /install/' . DIR_INDEX_FILE . ' は、インストール完了後にファイルを削除してください。');
}
}
在父类SC_Utils中大量使用了SC_Utils_EX中的函数,这是什么用法啊?又如下面这段代码:function searchInstallerPath($path) {
$installer = 'install/' . DIR_INDEX_PATH; if (SC_Utils_Ex::sfIsHTTPS()) {
$proto = "https://";
} else {
$proto = "http://";
}
$host = $proto . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
if ($path == '/') {
return $host . $path . $installer;
}
if (substr($path, -1, 1) != '/') {
$path .= $path . '/';
}
$installer_url = $host . $path . $installer;
$resources = fopen(SC_Utils_Ex::getRealURL($installer_url), 'r');
if ($resources === false) {
$installer_url = SC_Utils_Ex::searchInstallerPath($path . '../');
}
return $installer_url;
}上面的代码中的黄色部分,也就是父类在实现函数searchInstallerPath的使用,如果$resources === false ,那么就调用子类SC_Utils_Ex的searchInstallerPath,但是子类SC_Utils_EX的searchInstallerPath函数是继承自父类SC_Utils的searchInstallerPath函数的啊,如此一来,似乎有点递归的意思了。求助啊,真是很迷茫。
class SC_Utils_Ex extends SC_Utils {
}class SC_Utils { // インストール初期処理
function sfInitInstall() {
// インストール済みが定義されていない。
if (!defined('ECCUBE_INSTALL')) {
$phpself = $_SERVER['PHP_SELF'];
if( !ereg('/install/', $phpself) ) {
$path = substr($phpself, 0, strpos($phpself, basename($phpself)));
$install_url = SC_Utils_Ex::class SC_Utils_Ex($path);
header('Location: ' . $install_url);
exit;
}
}
$path = HTML_REALDIR . 'install/' . DIR_INDEX_FILE;
if(file_exists($path)) {
SC_Utils_Ex::sfErrorHeader('>> /install/' . DIR_INDEX_FILE . ' は、インストール完了後にファイルを削除してください。');
}
}
在父类SC_Utils中大量使用了SC_Utils_EX中的函数,这是什么用法啊?又如下面这段代码:function searchInstallerPath($path) {
$installer = 'install/' . DIR_INDEX_PATH; if (SC_Utils_Ex::sfIsHTTPS()) {
$proto = "https://";
} else {
$proto = "http://";
}
$host = $proto . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
if ($path == '/') {
return $host . $path . $installer;
}
if (substr($path, -1, 1) != '/') {
$path .= $path . '/';
}
$installer_url = $host . $path . $installer;
$resources = fopen(SC_Utils_Ex::getRealURL($installer_url), 'r');
if ($resources === false) {
$installer_url = SC_Utils_Ex::searchInstallerPath($path . '../');
}
return $installer_url;
}上面的代码中的黄色部分,也就是父类在实现函数searchInstallerPath的使用,如果$resources === false ,那么就调用子类SC_Utils_Ex的searchInstallerPath,但是子类SC_Utils_EX的searchInstallerPath函数是继承自父类SC_Utils的searchInstallerPath函数的啊,如此一来,似乎有点递归的意思了。求助啊,真是很迷茫。
http://www.iteye.com/topic/410715
http://www.iteye.com/topic/319039
很怀疑是否偷懒的产物就是最初写了个类A,然后业务发展了,新增了一个类B,但要用到类A的功能,懒得重写就扩展
类A因为旧业务暂时还不能撤掉,又需要根据业务调整,增加了判断,静态调用B的方法
设计模式学习起步ing...不知道有没说错
由于SC_Utils_Ex 是继承了 SC_Utils,所以 SC_Utils_Ex::searchInstallerPath 也就可能有功能上的差异2、这个代码全部采用静态方式调用类的方法
所以 SC_Utils_Ex::searchInstallerPath 和 SC_Utils::searchInstallerPath 可以理解为不同命名空间中的同名函数3、在 SC_Utils::searchInstallerPath 中调用 SC_Utils_Ex::searchInstallerPath 确实有递归的嫌疑。但递归也并没有什么不妥的地方
function searchInstallerPath($path) {
...
SC_Utils_Ex::searchInstallerPath($path . '../') 是逐级向上处理路径
1.我在整个网站的工程里似乎都没有找到SC_Utils_EX类的实现代码,我想应该是没有区别吧。
2.何以见得这些代码全部是采用静态方式啊?似乎我没有看到static啊。
class SC_Utils_Ex extends SC_Utils {
}
那么这个 SC_Utils_Ex 是预留给以后扩展用的
这并不影响程序的执行2、形如 SC_Utils_Ex::searchInstallerPath 这样(类名::方法名)的使用就是以静态方式调用类的方法,与调用普通函数没有区别