Jump to content

Can't modify variable in parent class from child class


bulletseed

Recommended Posts

Hi.  I'm kinda new at extending classes.  I have tried searching all over this and other forums about this problem and I just haven't been able to figure it out. I have one class extending another and I'm trying to modify the value of a variable in the parent class. The problem is that once the function in the child class ends, the parent's variable that I just changed turns back into what it was before.

 

<?php
class _parent
{
public $var;

public function __construct()
{
	$this->var = 'OLD';
}

public function show()
{
	$child = new _child();
	$child->change();
	echo $this->var;
}
}

class _child extends _parent
{
public function change()
{
	$this->var = 'NEW';
}	
}

$parent = new _parent();
$parent->show();

/* Why is output not NEW ??? */
?>

 

What am I doing wrong? Or am I trying something that is not possible?

 

Thanks for your feedback.

 

-BulletSeed

Link to comment
Share on other sites

Ah right.

You create a new child. The variable that gets changed is the one in child, not the parent. If you want to change $parent's variable, you need to pass $parent to the function or have it return a value which you can then assign.

 

You have like this:

$a is parent.

$b is a variable inside $a;

make a change to $b, it doesn't affect $a.

 

Why do all your classes start with _? that's going to get annoying to type all the time.

Link to comment
Share on other sites

(The underscore is just there because in this example i wanted to use the 'parent' and 'child' names for the classes for clarity but parent is a reserved word in PHP. I don't use them in my real project.)

 

Well the thing is that i cant return that value in that function because in my real project I'm already returning something else there. I just wanted to know if what I am trying to do here (modifying a variable in a parent class from WITHIN a child class) was actually possible at all. I thought it would be.

Link to comment
Share on other sites

No, extends means the class includes all the variables and functions of the parent. It doesn't mean the $child is a pointer to $parent, which is how you're trying to use it, as if $child was $parent, so any changes you make to $child would apply to $parent. That's not what extends means.

Link to comment
Share on other sites

ahh, gotcha. hmmm... if that's how it works.. then...

 

...ok... i just tried doing the same thing by declaring the $var as static and referencing it with the '::' operator on both classes and it seems to work exactly like I wanted originally. I guess that's the correct way of doing what I was thinking.

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.