473,224 Members | 1,692 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,224 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 2420
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.