Jump to content

Recommended Posts

Hey guys, i having problem with extended classes accessing $this variables in PHP5.

 

Basically i have my main classes, which holds all my classes like database, template, application functions etc. I then have my modules, and when i construct the module i pass $class into it and store it as an instance eg:

 

class SomeClass
{
     protected $class;

     function __construct( $class )
     {
          $this->class =& $class;

         include( CLASS_PATH . 'class2.php' );
         $class2 = class2( );
    }
}

 

Then in class2 i have:

 

class SomeClass2 extends SomeClass
{
     function __construct( )
     {
         var_dump( $this );
     }
}

 

Now what happens is $this->class comes out as a null, but i want it to be the object i made an instance of. Just wondering first if this can be done, and secondly what am i doing wrong. Also if i set say: $this->someString = "this is some string", the dump will come out correctly for someString.

 

Thanks!

now this is a dangerous method to take.

 

the parent class is the only class that grabs this main class effort you talk about. when ever you instantiate a class that extends another and that parent class holds the main class object then you must not only call teh constructor of the parent class but also pas the resource to this main class...

 

In my opinion you woul dbe better off NOT building your model in this manner.  perhaps consider making teh methods of teh main class static where possible so they can be called from anywhere whithout the need to instantiate that class...

 

I have seen a few applications where there is one main class and everything else extends that class (or a child of that class) - this is totally unnessecary. The idea of a class is to encapsulate all associated pieces of data and methods into one entity so it can e be used all over the shop.

 

The main class does NOT have to contain all these properties which, by the sounds of it, could be legitimate classes in their own right.

I think what you are looking for is the singleton pattern. http://www.google.com/search?q=php+singleton&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

 

and in your example, class is going to be null because you never call the parent's constructor - parent::_construct; inside of the child constructor. Yeah it will return something wild as your approach doesnt seem too sound, but it gets something out of it.

thanks guys for the help.

 

yeah as i get more into the application im starting to realize the way i am doing it is a bad idea, i just trying trying avoid using globals, but making sure the class is constructed first.

 

Thanks for the help.

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.