473,320 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
3 2424
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Dan | last post by:
I have seen differing ways to pass values to a class: $db=new mysqlclass('localhost','user','passwd','database'); ..... OR $db=new mysqlclass() $db->host='localhost'; $db->user='user';...
3
by: scoomey | last post by:
Hi folks- I am currently working on a PHP application which I am using as a personal learning environment for getting a better working understanding about how to tackle application development...
3
by: Shawn Ferguson | last post by:
Hello All, I'm starting to learn C# and OOP to become a better programmer, however I;m getting frustrated. It's tough, but what is making it really tough for me is trying to do everything with...
13
by: Schmidty | last post by:
If you do a page reload with $_SERVER will your program lose a mysqli connection upon the reload of the page? Would this code work? I need to know how to carry over a connection between methods as...
22
by: amygdala | last post by:
Hi, I'm trying to grasp OOP to build an interface using class objects that lets me access database tables easily. You have probably seen this before, or maybe even built it yourself at some...
2
by: webcm123 | last post by:
People say that structural programming isn't good for database connection. I code fast-running structural oriented CMS and I don't know what I should do. I use mysql connection using mysql_*. I...
2
by: chromis | last post by:
Hi there, I've been reading an OOP book recently and it gives some nice Adaptor / Template patttern code to wrap around the php Mysql functions. I thought that I'd try and create a Simple Address...
11
by: moltendorf | last post by:
Hey everyone, as I have been by far pleased with some of the top helpers on this forum, I have decided to send another one your way. Even though I am personally having minimal issues with this...
3
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.