PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
bhvivarelli
Forum Newbie
Posts: 6 Joined: Fri Jul 06, 2007 1:01 pm
Post
by bhvivarelli » 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?
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
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Fri Jul 06, 2007 1:33 pm
Yes... error messages happen. That's when you debug. :-p
You are calling getId() here:
Code: Select all
$object_list[$i - 1]["id"]->getId();
And where do you assign the 'id' index to an object?
bhvivarelli
Forum Newbie
Posts: 6 Joined: Fri Jul 06, 2007 1:01 pm
Post
by bhvivarelli » Fri Jul 06, 2007 2:04 pm
I change the array key "a" for "id", sorry!
Code: Select all
$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]["a"]->getId();
$curr_index = $i;
if($prev_index == $curr_index) {
$object_list[$i - 1]["B"][] = $b;
} else {
$object_list[$i]["a"] = $a;
$object_list[$i]["B"][] = $b;
}
} else {
$object_list[$i]["a"] = $a;
$object_list[$i]["B"][] = $b;
}
$i++;
}
But I assign the "id" here
So when the "id" already exists in the previous position [$i - 1]["a"],the object "b" is put on key [$i - 1]["B"][]
Code: Select all
$prev_index = $object_list[$i - 1]["a"]->getId();
$curr_index = $i;
if($prev_index == $curr_index) {
$object_list[$i - 1]["B"][] = $b;
} else {
$object_list[$i]["a"] = $a;
$object_list[$i]["B"][] = $b;
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Fri Jul 06, 2007 2:20 pm
take a look at the output of
Code: Select all
$i = 0;
while($i < 5)
{
$a = new A(1);
$b = new B($i,$a);
if($i > 0) {
if ( !isset($object_list[$i - 1]) ) {
echo '$object_list has no element ' , $i-1;
die();
}
$prev_index = $object_list[$i - 1]["a"]->getId();
$curr_index = $i;
if($prev_index == $curr_index) {
echo "object_list[$i - 1][B][]<br />\n";
$object_list[$i - 1]["B"][] = $b;
}
else {
echo "object_list[$i][a] object_list[$i][B][]<br />\n";
$object_list[$i]["a"] = $a;
$object_list[$i]["B"][] = $b;
}
}
else {
echo "object_list[$i][a] object_list[$i][B][]<br />\n";
$object_list[$i]["a"] = $a;
$object_list[$i]["B"][] = $b;
}
$i++;
}
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Fri Jul 06, 2007 2:24 pm
You have the array structure outputted to the screen... What you're doing doesn't make sense to me, but it might to you. Look at the array structure. I don't see numerical indexes in your main array.
bhvivarelli
Forum Newbie
Posts: 6 Joined: Fri Jul 06, 2007 1:01 pm
Post
by bhvivarelli » Fri Jul 06, 2007 2:55 pm
How could have not set the $object[$i - 1]
Code: Select all
if ( !isset($object_list[$i - 1]) ) {
echo '$object_list has no element ' , $i-1;
die();
}
if I put this
Code: Select all
} else {
$object_list[$i]["id"] = $a;
$object_list[$i]["B"][] = $b;
}
echo '<pre>' . $i . " - ";
print_r($object_list[$i]);
echo '</pre>';
echo "<br>";
$i++;
and appear on screen the result
bhvivarelli
Forum Newbie
Posts: 6 Joined: Fri Jul 06, 2007 1:01 pm
Post
by bhvivarelli » Fri Jul 06, 2007 2:59 pm
The $i will be the numerical index of the array.
Code: Select all
$object_list[$i]["id"] = $a;
$object_list[$i]["B"][] = $b;
if $i = 0 then it looks like
$object_list[0]["id"] = $a;
$object_list[0]["B"][] = $b;
$i = 1
$object_list[1]["id"] = $a;
$object_list[1]["B"][] = $b;
.
.
.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Fri Jul 06, 2007 3:15 pm
Not quite.
object_list[0][a] object_list[0][]<br />
object_list[1 - 1][]<br />
$object_list has no element 1
You see, no object_list[1]
bhvivarelli
Forum Newbie
Posts: 6 Joined: Fri Jul 06, 2007 1:01 pm
Post
by bhvivarelli » Fri Jul 06, 2007 3:26 pm
The problem is here
It seems like that jump this script line.
But why?
bhvivarelli
Forum Newbie
Posts: 6 Joined: Fri Jul 06, 2007 1:01 pm
Post
by bhvivarelli » Fri Jul 06, 2007 3:47 pm
Thanks guys, but I found the solution.
Is just take the $i++ and put inside the elses statement.