【发布时间】:2017-07-20 17:38:49
【问题描述】:
我使用 CakePHP 2.9.8 开发了一个 7 年前编写并发展到现在的旧应用程序。 不幸的是,第一个开发人员在 CakePHP 的库中添加了一些代码,为了迁移到 CakePHP 的第 3 版,我需要在应用程序中进行更改。
其中一个变化是位于~\lib\Cake\Core\App.php 中的App::load,因为它使用了static::_map($file, $className, $plugin);,我可以编写一个扩展App.php 的类并重写_map 函数。
我的问题:
可以覆盖受保护的函数或属性吗? 如果没有:
为什么在 CakePHP 中它们像
static::一样被使用(或调用),例如:static::_map($file, $className, $plugin);但定义是protected static function _map($file, $name, $plugin = null)如果是:我应该在我的应用程序中的哪个位置定义扩展 App 的类 Foo 以及我想要删除开发人员更改的
load函数,我应该在哪里编写 Foo::load?
我在这里也放了App::load函数:
public static function load($className) {
if (!isset(static::$_classMap[$className])) {
return false;
}
if (strpos($className, '..') !== false) {
return false;
}
$parts = explode('.', static::$_classMap[$className], 2);
list($plugin, $package) = count($parts) > 1 ? $parts : array(null, current($parts));
$file = static::_mapped($className, $plugin);
if ($file) {
return include $file;
}
$paths = static::path($package, $plugin);
if (empty($plugin)) {
$appLibs = empty(static::$_packages['Lib']) ? APPLIBS : current(static::$_packages['Lib']);
$paths[] = $appLibs . $package . DS;
$paths[] = APP . $package . DS;
$paths[] = CAKE . $package . DS;
} else {
$pluginPath = CakePlugin::path($plugin);
$paths[] = $pluginPath . 'Lib' . DS . $package . DS;
$paths[] = $pluginPath . $package . DS;
}
$normalizedClassName = str_replace('\\', DS, $className);
// Start point of custom codes
// Load Custom Classes that are usually added during customizations
// This part is for accepting a class like **XControllerCustom** but the name of file is: **XController**
if($package === 'Model'){
foreach ($paths as $path) {
$file = $path . DS . $className . '.php';
$file_custom = $path . 'Custom' . DS . $normalizedClassName . '.php';
if (file_exists($file_custom) && file_exists($file)) {
self::_map($file_custom, $className);
include $file;
return include $file_custom;
}
}
}
// End of custom's code
foreach ($paths as $path) {
$file = $path . $normalizedClassName . '.php';
if (file_exists($file)) {
static::_map($file, $className, $plugin);
return include $file;
}
}
return false;
}
【问题讨论】:
-
1) 您可以覆盖所有受保护的方法。
标签: php function cakephp overriding late-static-binding