mb81 Posted June 21, 2007 Share Posted June 21, 2007 I had posted this probably a year ago and didn't get an answer that I could understand and actually worked and the ugly bug has reappeared, so I want to try again, maybe I have gotten smarter, maybe someone can use simple language for me. I have two "core" classes, siteclass and htmlclass. All of my core functions are in site class (queries, fetches, other common functions) and html class is a class I use for formatting of tables and forms. So, here is my OOP layout: siteclass htmlclass extends siteclass module1class extends htmlclass module2class extends htmlclass All of my other modules extend from htmlclass so they have the html and siteclass functions. Here is my problem: During the initialization of siteclass, I set configuration variables from the database. Then, I initialize htmlclass and my moduleclasses. When I try to access my configuration variables through my module classes, they do not exist. The variables that I have set for my siteclass do not extend to my htmlclass or my moduleclass. Any thoughts? Quote Link to comment Share on other sites More sharing options...
trq Posted June 22, 2007 Share Posted June 22, 2007 How are you accessing the properties? Quote Link to comment Share on other sites More sharing options...
KrisNz Posted June 22, 2007 Share Posted June 22, 2007 does the constructor for htmlclass call its parent constructor i.e the constructor for siteclass ? See if this somewhat lame example applies to your situation, otherwise post some code <?php class BigPoppa { var $dad; function BigPoppa() { $this->dad = "im ".__CLASS__."<br/>"; } } class OlderSibling extends BigPoppa { var $teen; function OlderSibling() { //without this call the 'dad' property wont get set, php will only //call or create 1 constructor for you parent::BigPoppa(); $this->teen = "im an ".__CLASS__." <br/>"; } } class littleKid extends OlderSibling { //no constructor, php calls OlderSibling constructor instead var $kid; function foobar() { echo $this->dad; echo $this->teen; echo "im a ".__CLASS__."<br/>"; } } $k = new littleKid(); $k->foobar(); ?> Quote Link to comment Share on other sites More sharing options...
Jenk Posted June 25, 2007 Share Posted June 25, 2007 child constructors do not call upon parent constructors unless you explicitly call them with the parent keyword. Quote Link to comment Share on other sites More sharing options...
pthurmond Posted July 19, 2007 Share Posted July 19, 2007 I know that if you use PHP5 and the __construct() function as your constructor any child classes that are initialized will automatically call upon the parent class (and functions) to add to its list of properties. Which also mentions that the parents constructor is automatically called and does whatever initialization that you have designed it to do. Check out this book: "PHP 5 Objects, Patterns, and Practice" by Matt Zandstra from Apress (apress.com) I have been using that book and it helps a lot. You can download the sample code from apress.com. I hope that helps! -Patrick Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.