我使用CakePHP 2.9.8開發一個7年前編寫並開發至今的舊應用程序。 不幸的是,第一個開發人員在CakePHP的庫中添加了一些代碼,並且遷移到CakePHP的第三版本,我需要在應用程序中傳輸更改。覆蓋一些函數cakephp(Late Static Bindings)
變化之一是在位於~\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)
如果是:凡在我的應用程序應該定義類美孚延伸應用程序和
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)您可以覆蓋所有受保護的方法。 – DanielO