Can anybody explain why this Error Message occur?

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

Post Reply
bhvivarelli
Forum Newbie
Posts: 6
Joined: Fri Jul 06, 2007 1:01 pm

Can anybody explain why this Error Message occur?

Post by bhvivarelli »

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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

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 »

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

Code: Select all

$a = new A(1);
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;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

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++;
}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

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 »

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 »

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;
.
.
.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

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 »

The problem is here

Code: Select all

$object_list[$i - 1]["B"][]     = $b;

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 »

Thanks guys, but I found the solution.
Is just take the $i++ and put inside the elses statement.
Post Reply