CakePHP 2.0 还需要在模型中声明 $name 吗?

问题描述 投票:0回答:2

我的印象是我们必须声明 - public $name = 'ModelName';在 PHP4 专用的模型中。 现在 cakephp 不再支持 PHP4 我认为模型中不再需要 $name 声明。 不过,食谱仍然有包含它的说明:http://book.cakephp.org/2.0/en/models.html

据我所知,如果没有它,模型也能正常工作。 它有什么用途,为什么需要它?

谢谢!

php cakephp
2个回答
2
投票

这只是早期食谱的剩余内容。例如,参见 http://book.cakephp.org/2.0/en/models/model-attributes.html#name,它提到了 PHP4 兼容性,尽管 CakePHP 2.0 不再适用于 PHP4。

所以,回答你的问题:不,你不必在模型中声明

$name


0
投票

您的任何课程都不需要 $name 。 甚至在 1.3 中也没有(无论如何,谁仍然使用 php4^^),但尤其是在 2.0 中。

我编写了一个增强的 UpgradeShell,它从所有类文件中删除了那些不必要的块: http://cakephp.lighthouseapp.com/projects/42648/tickets/2117-improvements-for-20-upgrade-shell 但这个新命令我还没有添加到票证补丁中。

我将该命令称为“名称”

/**
 * Remove name (lib, controller, model, view, component, behavior, helper, fixture)
 *
 * @return void
 */
public function name() {
    $libs = App::path('Lib');
    $views = App::path('views');
    $controllers = App::path('controllers');
    $components = App::path('components');
    $models = App::path('models');
    $helpers = App::path('helpers');
    $behaviors = App::path('behaviors');

    $this->_paths = array_merge($libs, $views, $controllers, $components, $models, $helpers, $behaviors);
    $this->_paths[] = TESTS . 'Fixture' . DS;

    if (!empty($this->params['plugin'])) {
        $pluginPath = App::pluginPath($this->params['plugin']);
        $this->_paths = array(
            $pluginPath . 'Lib' . DS,
            $pluginPath . 'Controller' . DS,
            $pluginPath . 'Controller' . DS . 'Component' .DS,
            $pluginPath . 'View' . DS,
            $pluginPath . 'View' . DS . 'Helper' . DS,
            $pluginPath . 'Model' . DS,
            $pluginPath . 'Model' . DS . 'Behavior' . DS,
            $pluginPath . 'Test' . DS . 'Fixture' . DS,
            $pluginPath . 'libs' . DS,
            $pluginPath . 'controllers' . DS,
            $pluginPath . 'controllers' . DS . 'components' .DS,
            $pluginPath . 'views' . DS,
            $pluginPath . 'views' . DS . 'helpers' .DS,
            $pluginPath . 'models' . DS,
            $pluginPath . 'models' . DS . 'behaviors' . DS,
            $pluginPath . 'tests' . DS . 'fixtures' . DS,
        );
    }

    $patterns = array(
        array(
            'remove var $name = ...;',
            '/\bvar\s*\$name\s*=\s*(.*);/',
            ''
        ),
        array(
            'remove public $name = ...;',
            '/\bpublic\s*\$name\s*=\s*(.*);/',
            ''
        ),
    );
    $this->_filesRegexpUpdate($patterns);
}
© www.soinside.com 2019 - 2024. All rights reserved.