...

View Full Version : Undefined object properties



CoolAsCarlito
10-11-2012, 07:14 PM
For some reason its saying that both the datetime_sent and sender_avatar properties are undefined. I don't see why this error is saying it when it is clearly with the print_r below.



A PHP Error was encountered

Severity: Notice
Message: Undefined property: stdClass::$datetime_sent
Filename: models/messages_model.php
Line Number: 72

A PHP Error was encountered

Severity: Notice
Message: Undefined property: stdClass::$sender_avatar
Filename: models/messages_model.php
Line Number: 74


There's a var_dump of an array at the bottom of my code. This is also included next.



array(4) {
[0]=>
object(stdClass)#29 (8) {
["message_id"]=>
string(1) "1"
["subject"]=>
string(12) "Test Message"
["datetime_sent"]=>
string(6) "1 week"
["attachments"]=>
NULL
["message_content"]=>
string(446) "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
["sender_name"]=>
string(16) "Jeffrey Davidson"
["sender_email_address"]=>
string(20) "xtremer360@yahoo.com"
["sender_avatar"]=>
string(82) "http://dev.kansasout...rs/avatar5.jpg"
}
[1]=>
object(stdClass)#30 (8) {
["message_id"]=>
string(1) "2"
["subject"]=>
string(18) "Testing PM Message"
["datetime_sent"]=>
string(19) "2012-09-22 18:27:25"
["attachments"]=>
string(37) "["file1.jpg","file2.jpg","file3.jpg"]"
["message_content"]=>
string(51) "This is jsut a test of the personal message system!"
["sender_name"]=>
string(11) "Frank Scott"
["sender_email_address"]=>
string(24) "frankscott@testemail.com"
["sender_avatar"]=>
NULL
}
[2]=>
object(stdClass)#31 (8) {
["message_id"]=>
string(1) "3"
["subject"]=>
string(16) "Testing Whatever"
["datetime_sent"]=>
string(19) "2012-10-04 05:03:09"
["attachments"]=>
NULL
["message_content"]=>
string(11) "dak;fdaf;ld"
["sender_name"]=>
string(11) "Frank Scott"
["sender_email_address"]=>
string(24) "frankscott@testemail.com"
["sender_avatar"]=>
NULL
}
[3]=>
object(stdClass)#32 (3) {
["attachments"]=>
array(3) {
[0]=>
array(2) {
["file_name"]=>
string(9) "file1.jpg"
["is_file"]=>
bool(false)
}
[1]=>
array(2) {
["file_name"]=>
string(9) "file2.jpg"
["is_file"]=>
bool(false)
}
[2]=>
array(2) {
["file_name"]=>
string(9) "file3.jpg"
["is_file"]=>
bool(false)
}
}
["datetime_sent"]=>
string(8) "42 years"
["sender_avatar"]=>
string(81) "http://dev.kansasout...ars/avatar.jpg"
}
}




/**
* Gets all or last $x number of personal messages of the specified user
*
* @param integer $user_id User ID of the user specified
* @param integer $limit Limit of how many messages to retrieve
* @return object/NULL
*/
public function get_personal_messages($user_id, $limit = NULL, $timezone)
{
$this->db->select('personal_messages.message_id');
$this->db->select('personal_messages.subject');
$this->db->select('personal_messages.datetime_sent');
$this->db->select('personal_messages.attachments');
$this->db->select('personal_messages.message_content');
$this->db->select('CONCAT(users.first_name, " ", users.last_name) AS sender_name', FALSE);
$this->db->select('users.email_address AS sender_email_address');
$this->db->select('user_profiles.user_avatar AS sender_avatar');
$this->db->from('personal_messages');
$this->db->join('users', 'users.user_id = personal_messages.from_user_id');
$this->db->join('user_profiles', 'users.user_id = user_profiles.user_id');
$this->db->where('personal_messages.to_user_id', $user_id);
if ($limit != NULL)
{
if (is_numeric($limit))
{
$this->db->limit($limit);
}
}
$query = $this->db->get();
$personal_messages = $query->result();
if (count($personal_messages) > 0)
{
for ($x = 0; $x < count($personal_messages); $x++)
{
$attachments = $personal_messages[$x]->attachments;
if ($this->functions_model->null_check($attachments) === FALSE)
{
$attachments = json_decode($attachments, TRUE);
for ($x = 0; $x < count($attachments); $x++)
{
$file_name = $attachments[$x];
if ($this->functions_model->is_file('assets/downloads/'.$file_name, FALSE) === TRUE)
{
$attachments[$x] = array('file_name' => $file_name, 'is_file' => TRUE);
}
else
{
$attachments[$x] = array('file_name' => $file_name, 'is_file' => FALSE);
}
}
$personal_messages[$x]->attachments = $attachments;
}
$personal_messages[$x]->datetime_sent = $this->functions_model->actual_time('d F Y g:i a', $timezone, strtotime($personal_messages[$x]->datetime_sent));
$avatar = $this->functions_model->site_url().'assets/themes/'.$this->config->item('default_theme').'/images/avatars/avatar.jpg';
if ($this->functions_model->null_check($personal_messages[$x]->sender_avatar) === FALSE)
{
if ($this->functions_model->is_file('assets/themes/supr/images/avatars/'.$personal_messages[$x]->sender_avatar, FALSE) === TRUE)
{
$avatar = $this->functions_model->site_url().'assets/themes/'.$this->config->item('default_theme').'/images/avatars/'.$personal_messages[$x]->sender_avatar;
}
}
$personal_messages[$x]->datetime_sent = $this->functions_model->time_since(strtotime($personal_messages[$x]->datetime_sent));
$personal_messages[$x]->sender_avatar = $avatar;
}
}
echo '<pre>';
var_dump($personal_messages);
echo '</pre>';
die();
return $personal_messages;
}

CoolAsCarlito
10-11-2012, 08:12 PM
Something is wrong with this chunk of code because it is throwing attachments in its own first level array. So I need to figure out how I can work with the attatchments correctly to where it will apply the tasks below to each of the items in the first level array.



$attachments = $personal_messages[$x]->attachments;
if ($this->functions_model->null_check($attachments) === FALSE)
{
$attachments = json_decode($attachments, TRUE);
for ($x = 0; $x < count($attachments); $x++)
{
$file_name = $attachments[$x];
if ($this->functions_model->is_file('assets/downloads/'.$file_name, FALSE) === TRUE)
{
$attachments[$x] = array('file_name' => $file_name, 'is_file' => TRUE);
}
else
{
$attachments[$x] = array('file_name' => $file_name, 'is_file' => FALSE);
}
}
$personal_messages[$x]->attachments = $attachments;
}

Fou-Lu
10-11-2012, 08:16 PM
Ignore that for now.
What is the result of the structure on creation:


$personal_messages = $query->result();
print var_dump($personal_messages);

CoolAsCarlito
10-11-2012, 08:23 PM
Thank you for the quick reply.



array(3) {
[0]=>
object(stdClass)#29 (8) {
["message_id"]=>
string(1) "1"
["subject"]=>
string(12) "Test Message"
["datetime_sent"]=>
string(19) "2012-10-02 18:27:25"
["attachments"]=>
NULL
["message_content"]=>
string(446) "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
["sender_name"]=>
string(16) "Jeffrey Davidson"
["sender_email_address"]=>
string(20) "xtremer360@yahoo.com"
["sender_avatar"]=>
string(11) "avatar5.jpg"
}
[1]=>
object(stdClass)#30 (8) {
["message_id"]=>
string(1) "2"
["subject"]=>
string(18) "Testing PM Message"
["datetime_sent"]=>
string(19) "2012-09-22 18:27:25"
["attachments"]=>
string(37) "["file1.jpg","file2.jpg","file3.jpg"]"
["message_content"]=>
string(51) "This is jsut a test of the personal message system!"
["sender_name"]=>
string(11) "Frank Scott"
["sender_email_address"]=>
string(24) "frankscott@testemail.com"
["sender_avatar"]=>
NULL
}
[2]=>
object(stdClass)#31 (8) {
["message_id"]=>
string(1) "3"
["subject"]=>
string(16) "Testing Whatever"
["datetime_sent"]=>
string(19) "2012-10-04 05:03:09"
["attachments"]=>
NULL
["message_content"]=>
string(11) "dak;fdaf;ld"
["sender_name"]=>
string(11) "Frank Scott"
["sender_email_address"]=>
string(24) "frankscott@testemail.com"
["sender_avatar"]=>
NULL
}
}

Fou-Lu
10-11-2012, 08:31 PM
First line of the for loop, add:


printf("Evaluating record: %d" . PHP_EOL, $x);

Can you post the errors with that in it? We need to find out which one(s) this is coming off of.

CoolAsCarlito
10-11-2012, 08:35 PM
Response is this...

Evaluating record: 0 Evaluating record: 1 Evaluating record: 2

And there aren't any errors present. It was just the formation of adding in the logic with the attachments to the multidimensional array.

CoolAsCarlito
10-11-2012, 09:36 PM
Any more thoughts on the topic?

Fou-Lu
10-11-2012, 09:48 PM
Wait, you're original post has errors in it though. That's what I was looking for, to determine which one(s) of the records in the iteration threw those errors.
I mean to me it looks like the error is here:


$personal_messages[$x]->datetime_sent = $this->functions_model->actual_time('d F Y g:i a', $timezone, strtotime($personal_messages[$x]->datetime_sent));
$avatar = $this->functions_model->site_url().'assets/themes/'.$this->config->item('default_theme').'/images/avatars/avatar.jpg';
if ($this->functions_model->null_check($personal_messages[$x]->sender_avatar) === FALSE)


With a stdclass object, you shouldn't be receiving an error whilst writing to a property, only while attempting to retrieve one that hasn't yet been set. Above ::$datetime_sent is used in the strtotime, and ::$sender_avatar is used in null_check.

So what I was hoping to see was the record counter in conjunction with the above errors.


Evaluating Record: 1
A PHP Error was encountered

Severity: Notice
Message: Undefined property: stdClass::$sender_avatar
Filename: models/messages_model.php
Line Number: 74

And so forth.

Are you still getting that original error?

CoolAsCarlito
10-11-2012, 10:10 PM
Yes I am.

CoolAsCarlito
10-11-2012, 10:24 PM
The whole code block with the attachments is what's causing the errors because if I comment it out then the array works fine however I still need it to run the null check and also have it run the other tasks.

Fou-Lu
10-11-2012, 11:33 PM
The whole code block with the attachments is what's causing the errors because if I comment it out then the array works fine however I still need it to run the null check and also have it run the other tasks.

That makes no sense. The error itself isn't related to the use of the attachments, it indicates an issue with the datetime_sent and sender_avatar.
Can you confirm which lines are 72 and 74?

CoolAsCarlito
10-11-2012, 11:46 PM
Then why is it when I remove attachments code I no longer get those error messages

Fou-Lu
10-12-2012, 02:08 AM
I don't know, it doesn't make sense that is the error you would receive by a block that alters a different property. The only time you should hit an error is if you completely reassign the object itself, or if you read that property, and you don't do either until after that block of code.

CoolAsCarlito
10-12-2012, 02:32 AM
What's the problem then?

CoolAsCarlito
10-12-2012, 05:30 PM
Any more ideas?

Fou-Lu
10-12-2012, 06:41 PM
Any more ideas?

Yep, add a var_dump($personal_messages); immediately before this line: $personal_messages[$x]->datetime_sent = $this->functions_model->actual_time('d F Y g:i a', $timezone, strtotime($personal_messages[$x]->datetime_sent));

CoolAsCarlito
10-12-2012, 08:43 PM
array(3) {
[0]=>
object(stdClass)#29 (8) {
["message_id"]=>
string(1) "1"
["subject"]=>
string(12) "Test Message"
["datetime_sent"]=>
string(19) "2012-10-02 18:27:25"
["attachments"]=>
NULL
["message_content"]=>
string(446) "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
["sender_name"]=>
string(16) "Jeffrey Davidson"
["sender_email_address"]=>
string(20) "xtremer360@yahoo.com"
["sender_avatar"]=>
string(11) "avatar5.jpg"
}
[1]=>
object(stdClass)#30 (8) {
["message_id"]=>
string(1) "2"
["subject"]=>
string(18) "Testing PM Message"
["datetime_sent"]=>
string(19) "2012-09-22 18:27:25"
["attachments"]=>
string(37) "["file1.jpg","file2.jpg","file3.jpg"]"
["message_content"]=>
string(51) "This is jsut a test of the personal message system!"
["sender_name"]=>
string(11) "Frank Scott"
["sender_email_address"]=>
string(24) "frankscott@testemail.com"
["sender_avatar"]=>
NULL
}
[2]=>
object(stdClass)#31 (8) {
["message_id"]=>
string(1) "3"
["subject"]=>
string(16) "Testing Whatever"
["datetime_sent"]=>
string(19) "2012-10-04 05:03:09"
["attachments"]=>
NULL
["message_content"]=>
string(11) "dak;fdaf;ld"
["sender_name"]=>
string(11) "Frank Scott"
["sender_email_address"]=>
string(24) "frankscott@testemail.com"
["sender_avatar"]=>
NULL
}
}

Fou-Lu
10-12-2012, 09:05 PM
Are these lines 72 - 74 of models/messages_model.php?


$personal_messages[$x]->datetime_sent = $this->functions_model->actual_time('d F Y g:i a', $timezone, strtotime($personal_messages[$x]->datetime_sent));
$avatar = $this->functions_model->site_url().'assets/themes/'.$this->config->item('default_theme').'/images/avatars/avatar.jpg';
if ($this->functions_model->null_check($personal_messages[$x]->sender_avatar) === FALSE)


Actually, wait a second. This will be it here:


for ($x = 0; $x < count($personal_messages); $x++)
{
...
for ($x = 0; $x < count($attachments); $x++)
}

Hence why commenting out that block fixes it.
You are overwriting your $x. It's size will vary depending on the count of the current $attachments property. The retained value of $x is whatever the end result of count($attachments) -1 is.
I'm surprised that you never triggered a non-object error. The second iteration would place $x at 3, which would now evaluate false on the array and terminate the remaining items.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum