/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] OOP: Child Class Doesn’t Update Parent Variable

I’m fairly new to Object Oriented Programming, but I’m learning slowly. Basically what I have here is a parent class that calls a child class’s method which is supposed to update the parent class’s array ($this->lang). However when I add more data to the array in the child class, the change doesn’t go through to the parent class.

[B][SIZE=”3″]Rough Example:[/SIZE][/B]

[code=php]
class ClassA
{
protected $array;
public function __construct
{
$this->array = array();
}

protected function init()
{
$class2 = new ClassB;
$class2->insert_into_array();
}
}

class ClassB extends ClassA
{
public function __construct()
{
parent::__construct();
}
protected function insert_into_array()
{
array_push($this->array, $database_entry);
}
}

$heyo = new ClassA;
$heyo->init();
/*
I’m writing this code directly into this post and don’t have time to check it over carefully,
but it gives you an idea of what I’m trying to accomplish.
*/
[/code]

Now if I print_r the array in the child class’s insert_into_array() method, it shows up as expected. But if I print_r the array in the parent class’s init() method after calling the insert_into_array() method, it comes back empty.

So, the question is, how do I update the array in the parent class?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 05.2012 — I'm not 100% sure I understand what you mean by "the change doesn't go through to the parent class".

A child class inherits properties and methods from its parent class, but that relationship is only a class definition thing: it has nothing to do with [i]objects[/i] instantiated from either class -- which will know no more about each other than any other two objects would.

Or I'm misunderstanding what you're asking?
Copy linkTweet thisAlerts:
@ScottyBoyauthorFeb 05.2012 — Sorry, I used the wrong terminology here. I want to update the parent object's array, not class. For instance:

[code=php]
$classA = new ClassA;
$classB = new ClassB;

print_r($classA->array);
// No entries.

$classB->insert_into_array();

print_r($classA->array);
// $classA->array is still empty, but now $classB->array has new entries.
[/code]


So in calling the method of $classB, I want to update the arrays of both objects, not just $classB.
Copy linkTweet thisAlerts:
@NogDogFeb 05.2012 — Nope. There is no connection between the object instantiated from class A and that instantiated from class B. They don't know anything about each other (unless you tie them together, such as by having one of class B's variables contain an [i]object[/i] of class A -- and in that case only that specific instance of class A would be affected.)

Perhaps what you're looking for in this case is a [i]static[/i] class variable? (However, note that it then "belongs" to [i]all[/i] instances.)
[code=php]
<?php

class ClassA
{
protected static $array = array();

public function getArray()
{
return self::$array;
}
}

class ClassB extends ClassA
{
public function insertIntoArray($value)
{
array_push(self::$array, $value); // note use of self::
}
}

$testA = new ClassA();
$testB = new ClassB();

$testB->insertIntoArray('foo');
$testB->insertIntoArray('bar');

echo "<pre>".print_r($testA->getArray(), true)."</pre>n";
echo "<pre>".print_r($testB->getArray(), true)."</pre>n";

// However...

$testC = new ClassA();
$testD = new ClassB();
// static array already populated, since all objects of these classes will share it
echo "<pre>".print_r($testA->getArray(), true)."</pre>n";
echo "<pre>".print_r($testB->getArray(), true)."</pre>n";
[/code]
Copy linkTweet thisAlerts:
@ScottyBoyauthorFeb 05.2012 — That was exactly what I was looking for. Thank you once again for your expertise, NogDog. ?
×

Success!

Help @ScottyBoy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...