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(); } } Link to comment https://forums.phpfreaks.com/topic/198058-using-class-called-within-a-function-of-an-extended-class-problem/ 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. Link to comment https://forums.phpfreaks.com/topic/198058-using-class-called-within-a-function-of-an-extended-class-problem/#findComment-1039239 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. Link to comment https://forums.phpfreaks.com/topic/198058-using-class-called-within-a-function-of-an-extended-class-problem/#findComment-1039250 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? Link to comment https://forums.phpfreaks.com/topic/198058-using-class-called-within-a-function-of-an-extended-class-problem/#findComment-1039290 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. Link to comment https://forums.phpfreaks.com/topic/198058-using-class-called-within-a-function-of-an-extended-class-problem/#findComment-1039306 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. Link to comment https://forums.phpfreaks.com/topic/198058-using-class-called-within-a-function-of-an-extended-class-problem/#findComment-1039314 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.