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

Link to comment
Share on other sites

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
Share on other sites

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