Zogezegd zo gedaan. foo->Modify_Collection. Alleen is het resultaat niet wat ik verwacht had.
Vanuit de child roep ik de functie aan met parent::Modify_Collection('New_Name'); deze functie zou er dus moeten voorzorgen dat de vorige naam uit de object collection geunsetted wordt en er een nieuwe bijgestopt wordt.
Het probleem is nu dat ik wel $this gebruik in de functie van de parent, maar doordat ik de functie vanuit de child oproept, blijkt de $this te slaan op het child object... Met gevolgd dat de object collection uit de parent ongewijzigd blijft en object collection van het child wel gewijzigd wordt.
Wat kan ik hieraan doen ?
:: clsFooBar.php ::
<?php
class Foo {
public $Child_Collection = array();
function Modify_Collection($Old_Child_Name,$New_Child_Name) {
unset($this->Child_Collection[$Old_Child_Name]);
$this->Child_Collection[] = $New_Child_Name;
return TRUE;
}
function Create_Child($Child_Name) {
$Child_Instance = new Bar($Child_Name);
$this->Child_Collection[] = $Child_Name;
return $Child_Instance;
}
}
class Bar extends Foo {
public $Name = '';
function __construct($Child_name) {
$this->Name = $Child_name;
}
function Rename($New_Name) {
if (parent::Modify_Collection($this->Name,$New_Name)) {
$this->Name = $New_Name;
}
}
}
?>
:: index.php ::
<?php
error_reporting(E_ALL);
require_once('clsFooBar.php');
$foo_parent = new Foo;
$bar_child = $foo_parent->Create_Child('PHPhulp');
echo 'Bar Childs Name : ' . $bar_child->Name . '<br />';
echo 'Parent::Child Collection [voor Rename function]';
echo '<pre>' . print_r($foo_parent->Child_Collection,TRUE) . '</pre>';
echo '<hr />';
$bar_child->Rename('pluhPHP');
echo 'Parent::Child Collection [na Rename function]';
echo '<pre>' . print_r($foo_parent->Child_Collection,TRUE) . '</pre>';
echo '<hr />';
echo 'Bar Childs Name : ' . $bar_child->Name . '<br />';
echo 'Child::Child Collection [na Rename function]';
echo '<pre>' . print_r($bar_child->Child_Collection,TRUE) . '</pre>';
echo '<hr />';
?>
:: Output ::
Bar Childs Name : PHPhulp
Parent::Child Collection [voor Rename function]
Array
(
[0] => PHPhulp
)
Parent::Child Collection [na Rename function]
Array
(
[0] => PHPhulp
)
Bar Childs Name : pluhPHP
Child::Child Collection [na Rename function]
Array
(
[0] => pluhPHP
)