ZF中不可以使用__construct()构造方法,类的初始化操作需要用init()方法完成, 在php中,当子类有构造方法时,父类的构造方法不会被调用, 通过阅读zend/controller/Action.php的__construct()可以知道,它通过使用$this->init(),类似注入的方式,避免了这种情况。 那么在一个项目中,若干的controller可能会做一些同样的动作,比如初始化session、实例化一些对象等等, 如果在每个controller的init()中,写一些重复的代码,当controller比较多时,一来比较麻烦,二来不方便后期维护。 怎样解决这个问题呢?可以把这些代码写到一个文件中,然后include(),也可以OOXX,但我不认为这是个好办法,我的方法是,自定义一个父控制器。 例如:在library目录中新建一个Mylib/Controller/Parent.php文件,让这个文件继承zf的Controller,这样写: class Mylib_Controller_Parent extends Zend_Controller_Action { public function init() { parent::init(); // Your custom code } } 以后在每个项目controller中不要继承ZF的默认控制器了,直接继承我们自己的Parent: class DefaultController extends Mylib_Controller_Parent { public function init() { parent::init(); // Your custom code } }