Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Jayman777@gmail.com
Guest
 
Posts: n/a
#1: Oct 8 '06
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.


ZabMilenko
Guest
 
Posts: n/a
#2: Oct 8 '06

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



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.



Jayman777@gmail.com wrote:
Quote:
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.
>
C.
Guest
 
Posts: n/a
#3: Oct 8 '06

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



Jayman777@gmail.com wrote:
Quote:
>
$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.

Jayman
Guest
 
Posts: n/a
#4: Oct 8 '06

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


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.

Closed Thread