2017-07-20 55 views
0

我使用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

回答

1

你有一些嚴重缺乏在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

謝謝我的朋友 –