dongzhunqiu4841 2017-07-20 17:38
浏览 52
已采纳

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

I use CakePHP 2.9.8 to develop an old application that was written 7 years ago and developed until now. unfortunately the first developer add some codes in CakePHP's library and for migrating to the 3th version of CakePHP I need to transfer the changing in the application.

one of the changing is in the App::load located in ~\lib\Cake\Core\App.php and as it used static::_map($file, $className, $plugin);, I can write a class which extend App.php and rewrite the _map function.

My questions:

  1. Can be override a protected function or property? if no:

  2. why in CakePHP they were used (or called) like static:: for example: static::_map($file, $className, $plugin); but the definition is protected static function _map($file, $name, $plugin = null) if yes:

  3. Where in my application should I define class Foo which extend App and for load function that I want to remove the developer changes, where should I write Foo::load?.

I put also App::load function here:

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条回答 默认 最新

  • dongxieli3839 2017-07-20 20:16
    关注

    You have some serious lack of knowledge in OOP in php. Read these links carefully, they'll give you the answers and hopefully a complete understandig of both topics.

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

    Also there are plenty of questions and answers related to both on SO. Just search for them if the manual is not sufficient.


    For number three, there is no fixed place. Just put it in a Lib or Utility folder inside your app. However, I would choose an autoloader over the built in App stuff and just use uses to make my classes available. If your app doesn't use composer yet, just add it and use its autoader and namespaces. The only reason 2.x is still not using them is backward compatibility. App is a crutch to get namespace like functionality done without actually using them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起