P: n/a
|
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;
}
}
?> | |
Share this Question
P: n/a
|
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;
}
}
?> | |
P: n/a
|
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 | |
P: n/a
|
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; } }
?>
| | This discussion thread is closed Replies have been disabled for this discussion. | | Question stats - viewed: 2087
- replies: 3
- date asked: Jul 17 '05
| |