473,466 Members | 1,301 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem retrieving an object from an array and calling a method on the object

Hello, I am using PHP 4.4.1 and am having problems retrieving an object
from an array of objects and then calling a method on it. Here are two
simple classes that I am using:

class Day
{
var $day;
var $array_of_events;

function Day($in_ts)
{
$this->day = getdate($in_ts);
}

function addEvent($in_event)
{
$this->array_of_events[] = $in_event;
}
}

class Event
{
var $event_id;
var $begin_time;
var $end_time;
var $summary;
var $description;
var $category;

function Event($in_id, $in_begin_time, $in_end_time, $in_summary,
$in_description, $in_category)
{
$this->event_id = $in_id;
$this->begin_time = $in_begin_time;
$this->end_time = $in_end_time;
$this->summary = $in_summary;
$this->description = $in_description;
$this->category = $in_category;
}
}


The problem arises in another class's function with the following code:


$this->array_of_days = array();
for ($i = 1; $i < num_days_in_month + 1; $i++)
{
$this->array_of_days[$i] = new Day(mktime(00, 00, 00, $in_date['mon'],
$i, $in_date['year']));
}
/* Unrelated code */
$day_object = $this->array_of_days[$index];
$day_object->addEvent($event);


The program creates an error on the last line of the last code snippet.
My web host does not print out the error message so I don't know
exactly why it fails. I'm sure it has something to do with getting an
object from an array because I tried to call the addEvent function on a
Day object that was not retrieved from an array and it works fine.

Does anyone have an idea what the problem is? Thank you for any help.

Oct 8 '06 #1
3 1555

Try this:

$day_object =& $this->array_of_days[$index];
$day_object->addEvent($event);
Notice the ampersand next to the equals sign.
Also, comment out this "$day_object->addEvent($event);" and do this:

print '<pre>' . print_r($day_object, true) . '</pre>';

To get an idea of what might be there, if you are curious.

Ja*******@gmail.com wrote:
Hello, I am using PHP 4.4.1 and am having problems retrieving an object
from an array of objects and then calling a method on it. Here are two
simple classes that I am using:

class Day
{
var $day;
var $array_of_events;

function Day($in_ts)
{
$this->day = getdate($in_ts);
}

function addEvent($in_event)
{
$this->array_of_events[] = $in_event;
}
}

class Event
{
var $event_id;
var $begin_time;
var $end_time;
var $summary;
var $description;
var $category;

function Event($in_id, $in_begin_time, $in_end_time, $in_summary,
$in_description, $in_category)
{
$this->event_id = $in_id;
$this->begin_time = $in_begin_time;
$this->end_time = $in_end_time;
$this->summary = $in_summary;
$this->description = $in_description;
$this->category = $in_category;
}
}


The problem arises in another class's function with the following code:


$this->array_of_days = array();
for ($i = 1; $i < num_days_in_month + 1; $i++)
{
$this->array_of_days[$i] = new Day(mktime(00, 00, 00, $in_date['mon'],
$i, $in_date['year']));
}
/* Unrelated code */
$day_object = $this->array_of_days[$index];
$day_object->addEvent($event);


The program creates an error on the last line of the last code snippet.
My web host does not print out the error message so I don't know
exactly why it fails. I'm sure it has something to do with getting an
object from an array because I tried to call the addEvent function on a
Day object that was not retrieved from an array and it works fine.

Does anyone have an idea what the problem is? Thank you for any help.
Oct 8 '06 #2
C.

Ja*******@gmail.com wrote:
>
$this->array_of_days = array();
for ($i = 1; $i < num_days_in_month + 1; $i++)
{
$this->array_of_days[$i] = new Day(mktime(00, 00, 00, $in_date['mon'],
$i, $in_date['year']));
}
/* Unrelated code */
$day_object = $this->array_of_days[$index];
$day_object->addEvent($event);
It's kind of weird in PHP4 - even though objects are passed as copies
by default, arrays only seem to hold references. You could explicitly
assign a reference to the variable but unless you've got a good reason
to create another reference to the object, just use the one youv'e got
already:

$this->array_of_days[$index]->addEvent($event);

C.

Oct 8 '06 #3
I found out what the problem was. I followed ZabMilenko's advice and
printed out what was in the array and noticed it was blank. I went
back to the for loop earlier in the code and realized the loop is never
entered because that line should have read:

for ($i = 1; $i < $this->num_days_in_month + 1; $i++)

and not:

for ($i = 1; $i < num_days_in_month + 1; $i++)

Once that was corrected I had no problem using C's statement:

$this->array_of_days[$index]->addEvent($event);

Thank you for your help.

Oct 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Programatix | last post by:
Hi, I'm working on a project which includes WebServices and Windows Form application. The Windows Form application will call the WebServices to retrieve data from database. The data will be...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
3
by: jayderk | last post by:
Hello All, I am running in to a situation where the listbox is not refreshing for me. I am using a timer to cycle every second and call the timer_elapsed() event. in the time_elapsed event...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
0
by: shahiz | last post by:
This the error i get when i try to run my program Error: Unable to realize com.sun.media.amovie.AMController@18b81e3 Basically i have a mediapanel class that initialize and play the media as...
9
ADezii
by: ADezii | last post by:
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and not that often used approach, is the GetRows()...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.