PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

数据库初始化父类继承的问题

原创
2016-06-23 14:00:07 977浏览

小弟刚开始学ZendFramework框架,有个问题想请教各位大神

数据库初始化代码,我原先写在Bootstrap类中,是没有问题的。
写在控制器的init()方法里也可以。
但是我想抽象出一个类,并继承这个类,就是不行
总是报错 An error occurred Application error,错误日志也没有东西
我觉得应该是没有调用父类的init()方法导致没有初始化数据库造成的
但是应该怎么修改代码呢,求指教

下面是我的代码

1 父类 BaseController

class BaseController  extends Zend_Controller_Action{		public function init()	{		//初始化数据库适配器 		$url = constant("APPLICATION_PATH").DIRECTORY_SEPARATOR.'configs'.DIRECTORY_SEPARATOR.'application.ini'; 		$dbconfig = new Zend_Config_Ini($url,'mysql'); 		$db = Zend_Db::factory($dbconfig->db); 		$db->query('SET NAMES UTF8'); 		Zend_Db_Table::setDefaultAdapter($db); 		//echo "
"; 通过访问base控制器测试这里是有值的 		//print_r($db); 		//exit();	}		public function indexAction()	{	}}


2 IndexController
require_once   APPLICATION_PATH. '/models/tb_user.php';require_once  'BaseController.php';//有对数据库进行操作的控制器继承BaseController,没有对数据库进行操作的控制器继承Zend_Controller_Actionclass IndexController extends BaseController{    public function init()    {    	    	//把下面的注释就没有问题,放开第一句的注释就会报错An error occurred    Application error     	$user = new tb_user();          $res = $user->fetchAll()->toArray();   //查询数据表tb_user中的数据        $this->view->res = $res;  //将获得的数据分配给View        $this->render('index');    }    public function indexAction()    {        // action body    }


回复讨论(解决方案)

parent::init();//调用父类的构造函数试试
$user = new tb_user();

谢谢,正常了,可是还是有些疑问
必须显式调用父类构造函数才可以吗?
我是跟着视频教程里敲的代码,视频里没有显式调用父类构造函数也可以正常运行啊。。
难道是php版本的问题?
我的php版本是5.4.16

谢谢,正常了,可是还是有些疑问
必须显式调用父类构造函数才可以吗?
我是跟着视频教程里敲的代码,视频里没有显式调用父类构造函数也可以正常运行啊。。
难道是php版本的问题?
我的php版本是5.4.16
应该是php版本的问题

以前学过点 C#,在C#里继承是默认会先调用父类的构造函数,再调用子类构造函数的。

谢谢,原来是版本问题。

当然不是!是你理解的有问题
init 方法不是构造函数,但是会在 Zend_Controller_Action 的构造函数中调用,目的在于可以让用户干预一下默认的行为
你的 IndexController::init 方法覆盖了 BaseController::init 方法
于是 BaseController::init 中的初始化代码未能执行,所以出错

以前学过点 C#,在C#里继承是默认会先调用父类的构造函数,再调用子类构造函数的。
PHP新版本中子类不主动调用父类构造函数。并且这个在新版本中不是构造函数

当然不是!是你理解的有问题
init 方法不是构造函数,但是会在 Zend_Controller_Action 的构造函数中调用,目的在于可以让用户干预一下默认的行为
你的 IndexController::init 方法覆盖了 BaseController::init 方法
于是 BaseController::init 中的初始化代码未能执行,所以出错
对于他的版本应该是这样的。但是他学的视频会不会是PHP4的,那个时候构造函数名称可以自己定的,不一定是__construct

是吗?
构造函数 __construct 的确是 php5 才有的
但 php4 和 php5 都支持用与类名相同的方法作为构造函数
所以在这里无论如何都不能把 init 当做构造函数



当然不是!是你理解的有问题
init 方法不是构造函数,但是会在 Zend_Controller_Action 的构造函数中调用,目的在于可以让用户干预一下默认的行为
你的 IndexController::init 方法覆盖了 BaseController::init 方法
于是 BaseController::init 中的初始化代码未能执行,所以出错
对于他的版本应该是这样的。但是他学的视频会不会是PHP4的,那个时候构造函数名称可以自己定的,不一定是__construct

我知道php中构造函数是__constructor() ,init()并不是构造函数,但在初始化时调用。。。

我看的视频里也用__construtor做构造函数的。

我自己试了一下,把数据库初始化的代码写到 BaseController的__constructor()构造函数中,结果日志报错

[28-Mar-2014 10:00:06 Asia/Shanghai] PHP Fatal error: Declaration of BaseController::__construct() must be compatible with Zend_Controller_Action_Interface::__construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = Array) in E:\phpdoc\zend\promvc\application\controllers\BaseController.php on line 27

[28-Mar-2014 10:00:06 Asia/Shanghai] PHP Stack trace:

[28-Mar-2014 10:00:06 Asia/Shanghai] PHP 1. {main}() E:\phpdoc\zend\promvc\public\index.php:0

[28-Mar-2014 10:00:06 Asia/Shanghai] PHP 2. Zend_Application->run() E:\phpdoc\zend\promvc\public\index.php:26

[28-Mar-2014 10:00:06 Asia/Shanghai] PHP 3. Zend_Application_Bootstrap_Bootstrap->run() E:\phpdoc\zend\promvc\library\Zend\Application.php:366

[28-Mar-2014 10:00:06 Asia/Shanghai] PHP 4. Zend_Controller_Front->dispatch($request = *uninitialized*, $response = *uninitialized*) E:\phpdoc\zend\promvc\library\Zend\Application\Bootstrap\Bootstrap.php:101

[28-Mar-2014 10:00:06 Asia/Shanghai] PHP 5. Zend_Controller_Dispatcher_Standard->dispatch($request = class Zend_Controller_Request_Http { protected $_paramSources = array (0 => '_GET', 1 => '_POST'); protected $_requestUri = '//m.sbmmt.com/m/'; protected $_baseUrl = ''; protected $_basePath = NULL; protected $_pathInfo = '//m.sbmmt.com/m/'; protected $_params = array ('controller' => 'index', 'action' => 'index', 'module' => 'default'); protected $_rawBody = NULL; protected $_aliases = array (); protected $_dispatched = TRUE; protected $_module = 'default'; protected $_moduleKey = 'module'; protected $_controller = 'index'; protected $_controllerKey = 'controller'; protected $_action = 'index'; protected $_actionKey = 'action' }, $response = class Zend_Controller_Response_Http { protected $_body = array (); protected $_exceptions = array (); protected $_headers = array (); protected $_headersRaw = array (); protected $_httpResponseCode = 200; protected $_isRedirect = FALSE; protected $_renderExceptions = FALSE; public $headersSentThrowsException = TRUE }) E:\phpdoc\zend\promvc\library\Zend\Controller\Front.php:954

[28-Mar-2014 10:00:06 Asia/Shanghai] PHP 6. Zend_Controller_Dispatcher_Standard->loadClass($className = 'IndexController') E:\phpdoc\zend\promvc\library\Zend\Controller\Dispatcher\Standard.php:275

[28-Mar-2014 10:00:06 Asia/Shanghai] PHP 7. include_once() E:\phpdoc\zend\promvc\library\Zend\Controller\Dispatcher\Standard.php:357

把数据库初始化的代码写到 BaseController的__constructor()
那就覆盖了 Zend_Controller_Action::__constructor 当然是要出错的
php 中,如果子类有构造函数,则不执行父类的构造函数
要向执行的话,需要有 parent::__construct();

话说 Zend Framework 中,表属于 Mode
你为什么不按他的套路来呢?

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。