473,789 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ 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_event s;

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

function addEvent($in_ev ent)
{
$this->array_of_event s[] = $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_day s = array();
for ($i = 1; $i < num_days_in_mon th + 1; $i++)
{
$this->array_of_day s[$i] = new Day(mktime(00, 00, 00, $in_date['mon'],
$i, $in_date['year']));
}
/* Unrelated code */
$day_object = $this->array_of_day s[$index];
$day_object->addEvent($even t);


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 1568

Try this:

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

print '<pre>' . print_r($day_ob ject, 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_event s;

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

function addEvent($in_ev ent)
{
$this->array_of_event s[] = $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_day s = array();
for ($i = 1; $i < num_days_in_mon th + 1; $i++)
{
$this->array_of_day s[$i] = new Day(mktime(00, 00, 00, $in_date['mon'],
$i, $in_date['year']));
}
/* Unrelated code */
$day_object = $this->array_of_day s[$index];
$day_object->addEvent($even t);


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_day s = array();
for ($i = 1; $i < num_days_in_mon th + 1; $i++)
{
$this->array_of_day s[$i] = new Day(mktime(00, 00, 00, $in_date['mon'],
$i, $in_date['year']));
}
/* Unrelated code */
$day_object = $this->array_of_day s[$index];
$day_object->addEvent($even t);
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_day s[$index]->addEvent($even t);

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_mo nth + 1; $i++)

and not:

for ($i = 1; $i < num_days_in_mon th + 1; $i++)

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

$this->array_of_day s[$index]->addEvent($even t);

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
2256
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 returned as DataSet. Now, here's the problem. On .NET Framework 1.1, if any rows in the dataset returned contain errors (marked by calling the SetColumnError() method or
28
20346
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()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object doesn't support this property or method." in IE, and nothing happens in Firebird.
16
25419
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 the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
3
1974
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 Method I have code that checks to see if a DB table has been updated.. if so the new new data is uploaded to the listbox by calling lstbox1.datasource = myDBDataSet;
0
3942
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
2
4454
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 */ #include <time.h>
0
3491
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 datasource import java.awt.BorderLayout; import java.awt.Component; import java.io.*; import java.net.URL; import javax.media.*;
9
35558
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() Method of the Recordset Object. This Method varies slightly from DAO to ADO, so for purposes of this discussion, we'll be talking about DAO Recordsets. The ADO approach will be addressed in the following Tip. We'll be using a Query, consisting of 5...
275
12409
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
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10195
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6765
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
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 we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.