A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearIs Superclass's constructor run inside child constructor in PHP? - page 1
User InfoPosts
Is Superclass's constructor run inside child constructor in PHP?#1
If I have this class:

<?php
class Model
{
var $db;

function Model()
{
$this->db=new Db_Class();
}
}
?>


and a second class that extended the parent class:

<?php
class LessonModel extends Model
{

public function LessonModel()
{
//code here
}

public function getTitle($id)
{
$this->db->setTable((myTable();
return $this->db->get((title(,$id);
}
}
?>


Is it safe to assume that the $LessonModel->db field would have been instantiated by the parent Model class(s constructor, or do I need to run it manually using something like parent::Model();?

posted date: 2009-04-10 11:11:00


Re: Is Superclass's constructor run inside child constructor in PHP?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-10 11:11:01


Re: Is Superclass's constructor run inside child constructor in PHP?#3
In the given example, you can assume that.However, if your child class defines a constructor, PHP will not implicitly call it(s parent constructor.In order to do that, call:parent::__construct();

posted date: 2009-04-10 11:15:00


Re: Is Superclass's constructor run inside child constructor in PHP?#4
There(s a cheap way to get around having to call the parent constructor.Create an empty init() method in the base class. Call $this->init() in the base class constructor.All sub-classes can implement init() if they want to run code when the class is created.

posted date: 2009-04-10 11:20:00


Re: Is Superclass's constructor run inside child constructor in PHP?#5
You cannot assume that the parent constructor has been called because you have overridden this in your subclass. You would need to call parent::Model() as you suggest. If you change the class you are inheriting from you would obviously need to change this.If you are using PHP5 then you can name your constructors __construct(). This has the benefit of letting you easily call a parent constructor by doing parent::__construct() in any derived class without specifying the parent class name. You can then rearrange your classes( inheritance with less hassle and less danger of introducing obscure bugs.

posted date: 2009-04-10 11:41:00


Re: Is Superclass's constructor run inside child constructor in PHP?#6
His child class has defined a constructor using the older naming convention Classname()

posted date: 2009-04-10 11:43:00


Re: Is Superclass's constructor run inside child constructor in PHP?#7
Seriously the more I program on PHP the more I hate it! What the hell! I have almost the same class structure as the given example. I'm using the code for database connection. According to what I've seen here I have to initialize a new connection every time I need to do a query ?! I'm totally starting to think changing to Java! @Click Upvote Yeah... :/

posted date: 2011-04-08 05:47:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.