Database initialization problem with parent class inheritance

Source: Internet
Author: User
Tags stack trace zend framework
Little brother just started to learn zendframework framework, there is a question to ask you big God

The database initialization code, which I originally wrote in the Bootstrap class, is no problem.
It can also be written in the Init () method of the controller.
But I want to abstract out a class, and inherit this class, just can't
Always error "an ' error occurred application errors, there is nothing wrong with the log.
I think it should be. The init () method that did not call the parent class caused the database to not initialize
But how to change the code, ask for advice

Here's my Code.

1 Parent Class Basecontroller
Class Basecontroller extends Zend_controller_action{public function init () {//Initialize 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::setdefaultadapter ($DB); echo "
"; By accessing the base controller test here is the value of the//print_r ($DB); Exit ();} Public Function Indexaction () {}}


2 Indexcontroller
Require_once   Application_path. '/models/tb_user.php '; require_once  ' basecontroller.php ';//controller that has operations on the database Inherits Basecontroller, Controllers that do not operate on the database inherit Zend_controller_actionclass Indexcontroller extends basecontroller{public    function init ()    {        //The note below is no problem, let go of the first sentence of the comment will be an error occurred    application error     $user = new Tb_user ();          $res = $user->fetchall ()->toarray ();   Query data table tb_user data        $this->view->res = $res;  Assign the obtained data to the view        $this->render (' index ');    }    Public Function indexaction ()    {        //action body    }


Reply to discussion (solution)

Parent::init ();//Call the constructor of the parent class to try
$user = new Tb_user ();

Thanks, it's normal, but there's still some questions.
Do you have to call the parent class constructor explicitly?
I am following the video tutorial in the code, the video does not explicitly call the parent class constructor can also run correctly ah.
is the PHP version of the problem?
My PHP version is 5.4.16

Thanks, it's normal, but there's still some questions.
Do you have to call the parent class constructor explicitly?
I am following the video tutorial in the code, the video does not explicitly call the parent class constructor can also run correctly ah.
is the PHP version of the problem?
My PHP version is 5.4.16
Should be a PHP version of the problem

Before I learned some C #, inheritance in C # is by default calling the constructor of the parent class before calling the subclass constructor.

Thank you, it turned out to be a version issue.

Of course not! Yes, you understand, there's a problem.
The Init method is not a constructor, but is called in the constructor of Zend_controller_action to allow the user to intervene in the default behavior
Your Indexcontroller::init method covers the Basecontroller::init method.
So the initialization code in Basecontroller::init failed to execute, so an error occurred

Before I learned some C #, inheritance in C # is by default calling the constructor of the parent class before calling the subclass constructor.
The new version of PHP does not actively call the parent class constructor. And this is not a constructor in the new version.

Of course not! Yes, you understand, there's a problem.
The Init method is not a constructor, but is called in the constructor of Zend_controller_action to allow the user to intervene in the default behavior
Your Indexcontroller::init method covers the Basecontroller::init method.
So the initialization code in Basecontroller::init failed to execute, so an error occurred
That should be the case for his version. But he learned the video will not be PHP4, at that time the constructor name can be fixed, not necessarily __construct

Is it?
Constructor __construct It's really php5.
But both PHP4 and PHP5 support using the same method as the class name as the constructor
So there's no way to take Init as a constructor here anyway.



Of course not! Yes, you understand, there's a problem.
The Init method is not a constructor, but is called in the constructor of Zend_controller_action to allow the user to intervene in the default behavior
Your Indexcontroller::init method covers the Basecontroller::init method.
So the initialization code in Basecontroller::init failed to execute, so an error occurred
That should be the case for his version. But he learned the video will not be PHP4, at that time the constructor name can be fixed, not necessarily __construct

I know that the constructor in PHP is __constructor (), init () is not a constructor, but it is called when initializing ...

The video I watch also uses __construtor as a constructor.

I tried it myself and wrote the database initialization code into Basecontroller's __constructor () constructor, resulting in an error in the 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\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 (); protect ed $_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 Basecontroller's __constructor ()
Then cover the zend_controller_action::__constructor, of course, it's going to go wrong.
In PHP, if a subclass has a constructor, the constructor of the parent class is not executed
To be executed, a parent::__construct () is required;

In the Zend Framework, the table belongs to Mode
Why don't you follow his routine?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.