What Happened to my object hierarchy ???
| From: | Alan van den Bosch | Date: | Thu, 07 Oct 1999 11:30:35 +0000 |
| Subject: | What Happened to my object hierarchy ??? | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-5279@lists.php.net to get a copy of this message | ||
Hi,
Here is a curly one... I'm using recent cvs DSO build of php4beta on rh6.0
and I noticed the problem about a week ago, but it has taken a while to track
down.
I have found that in certain circumstances the contents of arrays get mutilated
for 'no apparent reason' (not that I can see anyways...). I have isolated the
behaviour, check out the following (also attached) example code, around line 55.
---------
<?php
class part{
function part($args=""){
$this->properties=array();
reset($args);
while($cur=each($args)){
$this->prop($cur['key'],$cur['value']);
}
}
function prop($var,$arg){
if (!isset($arg)){
// single argument, return current value
return $this->properties[$var];
}
// set and return new value
return $this->properties[$var]=$arg;
}
function show($indent=""){
echo "${indent}Properties of ".$this->prop("name").":\n";
$indent.="\t";
reset($this->properties);
while($cur=each($this->properties)){
//ignore index 'obj'
if ($cur['key']!="obj")echo "${indent}${cur['key']} =
${cur['value']} \n";
}
}
}
class assembly extends part{
function assembly($args="",$parts=""){
// call base constructor
$this->part($args);
$this->properties['obj']=array();
// add parts
$this->add($parts);
}
function add(&$parts){
reset($parts);
while($cur=each($parts)){
if (is_object($cur['value'])){
//
// uncomment only one of the following '$name=...' lines.
//
// this method of setting $name works
// $name=$cur['value']->properties["name"];
//
// this method of setting $name kills the contents of $this->properties...
$name=$cur['value']->prop("name");
//
$this->properties['obj'][$name]=$cur['value'];
}
}
}
function show($indent=""){
// list properties first
part::show($indent);
if (!is_array($this->properties['obj'])) return;
// list member objects
echo "${indent}member count is
".sizeof($this->properties['obj'])."\n";
echo "${indent}Members of ".$this->prop("name").":\n";
$indent.="\t";
reset($this->properties['obj']);
while($cur=each($this->properties['obj'])){
if (is_object($cur['value'])){
$cur['value']->show($indent);
}
else{
echo "\nshould be an object, value is
'".$cur['value']."'\n";
}
}
}
}
$door=new assembly(
array(
"name"=>"door",
"colour"=>"green"
),
array(
new part(
array(
"name"=>"panel",
"material"=>"wood"
)
),
new assembly(
array(
"name"=>"handle",
"type"=>"round"
),
array(
new part(
array(
"name"=>"inside knob"
)
),
new part(
array(
"name"=>"outside knob"
)
),
new assembly(
array(
"name"=>"lock"
),
array(
new part(
array(
"name"=>"barrel"
)
),
new part(
array(
"name"=>"bolt"
)
)
)
)
)
),
new assembly(
array(
"name"=>"hindge",
"size"=>"medium"
),
array(
new part(
array(
"name"=>"panel flap"
)
),
new part(
array(
"name"=>"frame flap"
)
),
new part(
array(
"name"=>"pin"
)
)
)
)
)
);
$door->show();
---------
Is it something I'm doing (if so, what ????), or is there a bug at work here ?
Regards
Alan van den Bosch
Sanguis Pty Ltd.