Roaches Posted April 9, 2010 Share Posted April 9, 2010 I'm trying to initialize a class within a function that is inside a class and use that class within an extended class, but having a problem with the extended class not being able to find it. Not the exact code I'm using, but same structure: class A { function test() { $this->class = new class; } } class B extends A { function B() { $this->class2 = new C; } } class C extends B { function C() { $this->class->function(); } } But I get a "Call to a member function function() on a non-object" error. It works if I do this, but I would prefer not to have to do it if I don't have do: class C extends B { function C() { global $a; $a->class->function(); } } Quote Link to comment Share on other sites More sharing options...
trq Posted April 9, 2010 Share Posted April 9, 2010 You really need to have another look at how relationships work. Your code really doesn't make allot of sense. Quote Link to comment Share on other sites More sharing options...
Roaches Posted April 9, 2010 Author Share Posted April 9, 2010 You really need to have another look at how relationships work. Your code really doesn't make allot of sense. I just realized that I forgot to add the fact that the test function inside of class A was called prior to class C being initialized, so it wasn't trying to pull the class out of a function that wasn't even called. Sorry about that. Quote Link to comment Share on other sites More sharing options...
andrewgauger Posted April 9, 2010 Share Posted April 9, 2010 $this->class = new class what does this mean? What are you expecting it to do? Quote Link to comment Share on other sites More sharing options...
Roaches Posted April 9, 2010 Author Share Posted April 9, 2010 $this->class = new class what does this mean? What are you expecting it to do? Assign class "class" to $this->class variable, which is does do, accessing it with $this through a child class after that function has been called is the problem. Quote Link to comment Share on other sites More sharing options...
trq Posted April 9, 2010 Share Posted April 9, 2010 Again, you need to look at how relationships work. Why would you need C within B when C inherits from B? Your entire approach is floored. 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.