Jump to content

Extends is not extending variables


mb81

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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();

?>

Link to comment
Share on other sites

  • 4 weeks later...

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.