Re: Include and extending classes
| From: | Matthew Weier O'Phinney | Date: | Thu, 16 Jun 2005 18:43:21 +0000 |
| Subject: | Re: Include and extending classes | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-217095@lists.php.net to get a copy of this message | ||
* Mike Smith <mikeosmith@gmail.com>:
> In core.class.php I have my generic special html methods and my db
> connection. I'm having trouble accessing $this->db (initialized in
> core.class.php) from user.class.php. Am I wrong in thinking
> user.class.php should be able to access methods/attributes of
> core.class.php?
> I'm running PHP 5.0.4 on Windows
>
> //core.class.php
>
> class core {
>
> var $db;
>
> function core(){
> //initialize db connection (works from here)
> }
> //Other methods
> }
>
> //system.class.php
> include "../../inc/core.class.php";
> class system extends core {
>
> function system(){
>
> }
> }
>
> //user.class.php
> include "system.class.php";
> class user extends system{
>
> function user(){
> //$this->db cannot be used here
> }
> }
Since you're using PHP5, you should use the magic method __construct()
for your constructors. Then, within each child class' constructor, add
the following:
parent::__construct();
By doing so, eventually the core class' constructor will be triggered.
--
Matthew Weier O'Phinney | WEBSITES:
Webmaster and IT Specialist | http://www.garden.org
National Gardening Association | http://www.kidsgardening.com
802-863-5251 x156 | http://nationalgardenmonth.org
mailto:matthew@garden.org | http://vermontbotanical.org