小弟刚开始学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
你为什么不按他的套路来呢?

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment
