覆盖一些函数cakephp(Late Static Bindings)

问题描述:

我使用CakePHP 2.9.8开发一个7年前编写并开发至今的旧应用程序。 不幸的是,第一个开发人员在CakePHP的库中添加了一些代码,并且迁移到CakePHP的第三版本,我需要在应用程序中传输更改。覆盖一些函数cakephp(Late Static Bindings)

变化之一是在位于~\lib\Cake\Core\App.phpApp::load和,因为它使用static::_map($file, $className, $plugin);,我可以写延伸App.php一个类和重写_map功能。

我的问题:

  1. 可以覆盖一个受保护的功能或属性? 如果没有:

  2. 为什么CakePHP中,他们使用(或调用)像static::例如:static::_map($file, $className, $plugin);但定义为protected static function _map($file, $name, $plugin = null) 如果是:

  3. 凡在我的应用程序应该定义类美孚延伸应用程序和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

1)您可以覆盖所有受保护的方法。 – DanielO

你有一些严重缺乏在PHP的面向对象的知识。仔细阅读这些链接,他们会给你答案,并希望对这两个主题有完整的理解。

  1. http://php.net/manual/en/language.oop5.visibility.php
  2. http://php.net/manual/en/language.oop5.late-static-bindings.php

还有一些涉及到双方的SO问题和答案大量。只要手册不够用就搜索它们。


对于第三,没有固定的地方。只需将它放入应用程序内的LibUtility文件夹即可。不过,我会选择内置App内容的自动加载器,并使用uses来提供我的课程。如果您的应用程序尚未使用作曲家,只需将其添加并使用其自动播放器和命名空间即可。 2.x仍然没有使用的唯一原因是向后兼容。 App是一种拐杖,可以在不实际使用它们的情况下完成名称空间功能。

+0

我只是不知道我们可以重写在PHP中的保护功能和**不是一些在PHP **中严重缺乏OOP知识。感谢您的链接和解释。 –

+0

我认为这很严肃,因为这在大概所有具有可见性范围关键字的支持OOP的语言中都是相同的。我认为这是一个严重缺乏这个原因。它有效地阻止您理解和编写您想要使用受保护或甚至私人的某些情况。这并不意味着冒犯。 – burzum

+0

谢谢我的朋友 –