搜尋
首頁後端開發php教程数据库初始化父类继承的问题

小弟刚开始学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 "<pre class="brush:php;toolbar:false">"; 通过访问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 = '/'; protected $_baseUrl = ''; protected $_basePath = NULL; protected $_pathInfo = '/'; 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
可以在PHP會話中存儲哪些數據?可以在PHP會話中存儲哪些數據?May 02, 2025 am 12:17 AM

phpsessionscanStorestrings,數字,數組和原始物。

您如何開始PHP會話?您如何開始PHP會話?May 02, 2025 am 12:16 AM

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

什麼是會話再生,如何提高安全性?什麼是會話再生,如何提高安全性?May 02, 2025 am 12:15 AM

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。

使用PHP會話時有哪些性能考慮?使用PHP會話時有哪些性能考慮?May 02, 2025 am 12:11 AM

PHP会话对应用性能有显著影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHP會話與Cookie有何不同?PHP會話與Cookie有何不同?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHP如何識別用戶的會話?PHP如何識別用戶的會話?May 01, 2025 am 12:23 AM

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

確保PHP會議的一些最佳實踐是什麼?確保PHP會議的一些最佳實踐是什麼?May 01, 2025 am 12:22 AM

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

PHP會話文件默認存儲在哪裡?PHP會話文件默認存儲在哪裡?May 01, 2025 am 12:15 AM

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!