Jump to content

Using class called within a function of an extended class problem


Roaches

Recommended Posts

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

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.

$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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.