467,166 Members | 1,027 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,166 developers. It's quick & easy.

OOP: fatal error with an array of objects

Hello,

what is wrong here? the purpose is to create an array of objects and then
interate over it,
calling some method from all of them.
I get just the following at the marked line ("// FATAL ERROR"):
Fatal error: Call to a member function on a non-object
See the code:
thank you for any hellpful hints!

<?php

$out="<h1>test for array input</h1><hr>";

$obj1=new Test ("FIRST");
$obj2=new Test ("SECOND");
$obj3=new Test ("THIRD");

//$array1=array($obj1, $obj2);
$array1[]=$obj1;
$array1[]=$obj2;

$out.="Listing with for: <br><br>";

for ($i=0;$i<sizeof ($array1);$i++)
{
$k=$array[$i];
$out.="calling name, i=$i: ".$k->toString(); // FATAL ERROR
}
class Test
{
var $name;
function Test ($name)
{

$this->name=$name;
}
function toString()
{
return "my name is: ".$this->name;
}

}

?>
Jul 17 '05 #1
  • viewed: 2192
Share:
3 Replies
Sorry, there was not the right code,

hier is it:
My real problem is adding a label element to a normal array, like this:
$array1[]=array("new"=>$obj3);

it works but then $obj3 seems to be corrupt
(Fatal error: call to a member func. of a non-obj)

<?php

$out="<h1>test for array input</h1><hr>";

$obj1=new Test ("FIRST");
$obj2=new Test ("SECOND");
$obj3=new Test ("THIRD");

//$array1=array($obj1, $obj2);
$array1[]=$obj1;
$array1[]=$obj2;

//$array1=array_merge ($array1, $p=array("dritter"=>$obj3));
$array1[]=array("new"=>$obj3);
$out.="Listing with for: <br><br>";
reset ($array1);
for ($i=0;$i<sizeof ($array1);$i++){
$k=$array1[$i];
$out.="calling name, i=$i: ".$k->toString()."<br>";
}

echo $out;

class Test {
var $name;
function Test ($name){

$this->name=$name;
}
function toString(){

return "my name is: ".$this->name;
}

}

?>
Jul 17 '05 #2
On 2004-03-03, PeterF wrote:
Sorry, there was not the right code,

hier is it:
My real problem is adding a label element to a normal array, like this:
$array1[]=array("new"=>$obj3);

it works but then $obj3 seems to be corrupt
(Fatal error: call to a member func. of a non-obj)

<?php

$out="<h1>test for array input</h1><hr>";

$obj1=new Test ("FIRST");
$obj2=new Test ("SECOND");
$obj3=new Test ("THIRD");

//$array1=array($obj1, $obj2);
$array1[]=$obj1;
$array1[]=$obj2;

//$array1=array_merge ($array1, $p=array("dritter"=>$obj3));
$array1[]=array("new"=>$obj3);
This line isn't doing what you think it's doing, you are inserting an
array into $array1[2] which contains an object. Hence the error below

$out.="Listing with for: <br><br>";
reset ($array1);
for ($i=0;$i<sizeof ($array1);$i++){
$k=$array1[$i];
$out.="calling name, i=$i: ".$k->toString()."<br>";
}
Here you call the toString() method which in the case of $array1[2] is
trying to call the method of an array as opposed to that of a Test
object.

Place a print_r($k) after $k=$array1[$i]; and run the code, then try
again with the line $array1[]=array("new"=>$obj3); commented out to see
what I mean.
echo $out;

class Test {
var $name;
function Test ($name){

$this->name=$name;
}
function toString(){

return "my name is: ".$this->name;
}

}

?>

I hope this helps.
--
Mike Peters
mike [-AT-] ice2o [-DOT-] com
http://www.ice2o.com
Jul 17 '05 #3
What you need is

$array1['new'] = $obj3;

and

foreach($array1 as $k) {
}

instead of the for() loop.

Uzytkownik "PeterF" <NO*************@pf-webservices.de> napisal w wiadomosci
news:c2**********@online.de...
Sorry, there was not the right code,

hier is it:
My real problem is adding a label element to a normal array, like this:
$array1[]=array("new"=>$obj3);

it works but then $obj3 seems to be corrupt
(Fatal error: call to a member func. of a non-obj)

<?php

$out="<h1>test for array input</h1><hr>";

$obj1=new Test ("FIRST");
$obj2=new Test ("SECOND");
$obj3=new Test ("THIRD");

//$array1=array($obj1, $obj2);
$array1[]=$obj1;
$array1[]=$obj2;

//$array1=array_merge ($array1, $p=array("dritter"=>$obj3));
$array1[]=array("new"=>$obj3);
$out.="Listing with for: <br><br>";
reset ($array1);
for ($i=0;$i<sizeof ($array1);$i++){
$k=$array1[$i];
$out.="calling name, i=$i: ".$k->toString()."<br>";
}

echo $out;

class Test {
var $name;
function Test ($name){

$this->name=$name;
}
function toString(){

return "my name is: ".$this->name;
}

}

?>

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Dan | last post: by
3 posts views Thread by scoomey | last post: by
3 posts views Thread by Shawn Ferguson | last post: by
13 posts views Thread by Schmidty | last post: by
22 posts views Thread by amygdala | last post: by
2 posts views Thread by webcm123@gmail.com | last post: by
3 posts views Thread by Scott Stark | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.