Can anybody explain why this Error Message occur?
Posted: Fri Jul 06, 2007 1:21 pm
Today I was writing and testing a code that I made using classes and I reiceve an Error Message. I try everything but I didn't find a answer for this, so I did a simple example with the same case and BAMMMM, error again. Please can anybody help me?
I get this on my screen:
Code: Select all
<?php
class A
{
private $id;
public function __construct($id)
{
$this->id = $id;
}
public function getId()
{
return $this->id;
}
}
class B
{
private $id;
private $a;
public function __construct($id,$a)
{
$this->id = $id;
$this->a = $a;
}
public function getId()
{
return $this->id;
}
public function getA()
{
return $this->a;
}
}
$i = 0;
while($i < 5)
{
$a = new A(1);
$b = new B($i,$a);
if($i > 0) {
echo '<pre>';
print_r($object_list[$i - 1]);
echo '</pre>';
echo "<br>";
$prev_index = $object_list[$i - 1]["id"]->getId();
$curr_index = $i;
if($prev_index == $curr_index) {
$object_list[$i - 1]["B"][] = $b;
} else {
$object_list[$i]["id"] = $a;
$object_list[$i]["B"][] = $b;
}
} else {
$object_list[$i]["id"] = $a;
$object_list[$i]["B"][] = $b;
}
$i++;
}
?>I get this on my screen:
Code: Select all
1 -
Array
(
[id] => A Object
(
[id:private] => 1
)
[B] => Array
(
[0] => B Object
(
[id:private] => 0
[a:private] => A Object
(
[id:private] => 1
)
)
)
)
2 - Fatal error: Call to a member function getId() on a non-object in C:\xampp\htdocs\bjsantos_teste\bjsantos\teste.php on line 49