重写某些函数cakephp(后期静态绑定)

重写某些函数cakephp(后期静态绑定),php,function,cakephp,overriding,late-static-binding,Php,Function,Cakephp,Overriding,Late Static Binding,我使用CakePHP2.9.8开发了一个7年前编写并开发到现在的老应用程序。 不幸的是,第一个开发人员在CakePHP的库中添加了一些代码,为了迁移到第三个版本的CakePHP,我需要在应用程序中传输更改 其中一个变化是位于~\lib\Cake\Core\App.php中的App::load,它使用的是static::\u map$file、$className、$plugin;,我可以编写一个类来扩展App.php并重写_map函数 我的问题是: 是否可以重写受保护的函数或属性? 如果否: 为

我使用CakePHP2.9.8开发了一个7年前编写并开发到现在的老应用程序。 不幸的是,第一个开发人员在CakePHP的库中添加了一些代码,为了迁移到第三个版本的CakePHP,我需要在应用程序中传输更改

其中一个变化是位于~\lib\Cake\Core\App.php中的App::load,它使用的是static::\u map$file、$className、$plugin;,我可以编写一个类来扩展App.php并重写_map函数

我的问题是:

是否可以重写受保护的函数或属性? 如果否:

为什么在CakePHP中使用或调用它们像static::例如:static::_map$file、$className、$plugin;但是定义是受保护的静态函数_map$file,$name,$plugin=null 如果是:

在我的应用程序中,我应该在哪里定义扩展应用程序的类Foo,对于要删除开发人员更改的加载函数,我应该在哪里编写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;
}

您严重缺乏php中OOP方面的知识。仔细阅读这些链接,它们会给你答案,希望你能完全理解这两个主题

还有很多问题和答案都与这两者有关。如果手册不够,只需搜索即可


第三,没有固定的位置。只需将其放在应用程序中的Lib或Utility文件夹中。然而,我会选择一个自动加载器而不是内置的应用程序,只使用uses使我的类可用。如果您的应用程序还没有使用composer,只需添加它并使用其autoader和名称空间即可。2.x仍然没有使用它们的唯一原因是向后兼容性。应用程序是在不使用名称空间的情况下完成类似名称空间的功能的支柱。

1您可以覆盖所有受保护的方法。我只是不知道我们可以覆盖PHP中的受保护函数,而不是严重缺乏PHP中OOP的知识。感谢链接和解释。我认为它是严肃的,因为在所有有OOP能力的语言中,都有相同的可见性范围关键字。我认为这是一个严重缺乏的原因。它有效地阻止了您理解和编写某些您想要使用受保护甚至私有的案例。这并不意味着冒犯。