在Doctrine中设置属性的正确方法?
问题描述:
在一些教程,他们告诉你要建立这样一个属性:在Doctrine中设置属性的正确方法?
$manager = Doctrine_Manager::getInstance();
Doctrine_Manager::getInstance()->setAttribute(
Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
,并在文件中就说明你:
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(
Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
我不知道应该用哪一个呢?不是后者吗?导致如何在第一个单元类中设置属性?是不是第二个更正确?
答
你甚至可以理解你正在看的代码吗?
第一个代码是“错误的”。首先分配Doctrine_Manager
对象$managger
,然后不使用这个变量。
如果你想在Doctrine_Manager
上做更多的事情,那么很自然地将这个引用赋值给不会让你的代码搞乱的东西。如果你想要做的只有一件事没有必要使用额外的变量,换句话说:
Doctrine_Manger::getInstance()->setAttribte(...);
或
$manager = Doctrine_Manger::getInstance();
$manager->setAttribute(...);
$manager->setAttribute(...);
$manager->doSth();
$manager->blahblahblah();