The problem of database initialization of parent class inheritance is that the younger brother has just started to learn the ZendFramework framework. if you have any questions, please contact us.
The database initialization code, which was originally written in the Bootstrap class, is correct.
It can also be written in the init () method of the controller.
However, if I want to abstract a class and inherit the class, it will not work.
An error occurred Application error is always reported, and there is nothing in the error log.
In my opinion, the init () method of the parent class should not be called, resulting in no database initialization.
But how can I modify the code?
Below is my code
1. parent class BaseController
Class BaseController extends Zend_Controller_Action {public function init () {// initialize the database adapter $ 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: setdefaadapter adapter ($ db); // echo""; Access the base controller to test whether there is a value // print_r ($ db); // exit ();} public function indexAction (){}}
2 IndexController
Require_once APPLICATION_PATH. '/models/tb_user.php'; require_once 'BaseController. php '; // The controller that has operations on the database inherits BaseController, and the controller that does not operate on the database inherits Zend_Controller_Actionclass IndexController extends BaseController {public function init () {// no problem with the following comments. An error occurred Application error $ user = new tb_user () will be reported when you release the comments in the first sentence (); $ res = $ user-> fetchAll ()-> toArray (); // query the data in the data table tb_user $ this-> view-> res = $ res; // assign the obtained data to View $ this-> render ('index');} public function indexAction () {// action body}
Reply to discussion (solution)Parent: init (); // call the constructor of the parent class.
$ User = new tb_user ();
Thank you. it's normal, but there are still some questions.
Must the parent class constructor be called explicitly?
I followed the code I typed in the video tutorial. The video can run normally without explicitly calling the parent class constructor ..
Is it the php version?
My php version is 5.4.16
Thank you. it's normal, but there are still some questions.
Must the parent class constructor be called explicitly?
I followed the code I typed in the video tutorial. The video can run normally without explicitly calling the parent class constructor ..
Is it the php version?
My php version is 5.4.16
It should be a php version.
I have learned C # Before. Inheritance in C # is a default function that calls the constructor of the parent class before calling the sub-class constructor.
Thank you. it turns out to be a version issue.
Of course not! You understand the problem.
The init method is not a constructor, but will be called in the constructor of Zend_Controller_Action to allow users to intervene in the default behavior.
Your IndexController: init method overwrites the BaseController: init method.
The initialization code in BaseController: init fails to be executed, so an error occurs.
I have learned C # Before. Inheritance in C # is a default function that calls the constructor of the parent class before calling the sub-class constructor.
In the new PHP version, child classes do not actively call the parent class constructor. This is not a constructor in the new version.
Of course not! You understand the problem.
The init method is not a constructor, but will be called in the constructor of Zend_Controller_Action to allow users to intervene in the default behavior.
Your IndexController: init method overwrites the BaseController: init method.
The initialization code in BaseController: init fails to be executed, so an error occurs.
This should be the case for his version. But will the video he learned be PHP4? at that time, the constructor name can be set by himself, not necessarily _ construct.
Yes?
Construct is indeed only available in php5.
However, both php4 and php5 support the same method as the class name as the constructor.
Therefore, init cannot be used as a constructor in any way.
Of course not! You understand the problem.
The init method is not a constructor, but will be called in the constructor of Zend_Controller_Action to allow users to intervene in the default behavior.
Your IndexController: init method overwrites the BaseController: init method.
The initialization code in BaseController: init fails to be executed, so an error occurs.
This should be the case for his version. But will the video he learned be PHP4? at that time, the constructor name can be set by himself, not necessarily _ construct.
I know that the constructor in php is _ constructor (). init () is not a constructor, but is called during initialization...
In my video, _ construtor is also used as the constructor.
I tried it myself and wrote the database initialization code to the _ constructor () constructor of BaseController. an error is returned in the result log.
[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. 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
Write the database initialization code to the _ constructor () of BaseController ()
It overwrites Zend_Controller_Action ::__ constructor. of course, an error is required.
In php, if the subclass has a constructor, the constructor of the parent class is not executed.
To be executed, you need to have parent :__ construct ();
In Zend Framework, the table belongs to the Mode.
Why don't you follow his routine?