Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Essentially, I have a message class with a function that can write to an array. I want to then return that instance of an array through a function when called.

This is the class:

class Message
{
    public $formMessages = array();

    public function __construct()
    {
    }

    public function writeFormMessage($field, $message)
    {
        $formMessages[$field] = $message;
    }

    public function getFormMessages()
    {
        return $this->formMessages;
    }
}

Here is how I am attempting to grab the formMessages array from another file. Yes I already have an instance of the Message class in said file.

$test = $message->getFormMessages();

It fails this predicate, though it doesn't seem to be seeing the array anyhow:

if (!empty($test))
{ 
}

The php error was 'Undefined variable: formMessages in C:\xampp\htdocs\test\classes\message.class.php on line 45'

Edit: Thanks all!

share|improve this question
    
...what's your question? What part doesn't work? Your question is unclear. –  Jeremy Jul 5 at 2:26
    
What's the output of var_dump($test); ? –  ccKep Jul 5 at 2:26
    
@ Jeremy, I'm receiving this error: PHP Notice: Undefined variable: formMessages in C:\xampp\htdocs\test\classes\message.class.php on line 45. Thanks. –  Error-404 Jul 5 at 2:28
    
formErrors isn't even in your posted code. Show us what/how you're using this. –  Fred -ii- Jul 5 at 2:28
2  
You need to access $formMessages through $this->formMessages. Take a look: ideone.com/wUCfkT (check my line 12 and compare to yours) –  Jeremy Jul 5 at 2:32

2 Answers 2

up vote 2 down vote accepted

Look at this line in your writeFormMessage method:

$formMessages[$field] = $message;

That attempts to access a local variable. (Which doesn't exist within that method.)

Compare to this usage in getFormMessages() however:

return $this->formMessages;

There you are correctly accessing the intended property.

Use the same $this-> syntax for both.

share|improve this answer
1  
Thank you for making this a community. Too many rep-leeching users -- even high-rep ones who should know by now -- that post answers like this non-wiki (see @1nflktd below for ref). –  Jeremy Jul 5 at 2:36
    
@Jeremy Actually, the other answer was put up before this one was put up. Both may have been typing out the same or similar answer at the very same time. Since your comment led to an answer, you also had the opportunity to have put in an answer of your own. –  Fred -ii- Jul 5 at 2:41
    
@Fred-ii- This one was posted 6 minutes ago, the below 5 minutes. –  Jeremy Jul 5 at 2:41
    
@Jeremy This one 2:35:16 - the other one 2:35:34 look at the timestamps. I doubt very much that the other answer copied on this one and/or your comment within an 18 second period. –  Fred -ii- Jul 5 at 2:44
1  
@Jeremy My point is; if answers are to be given in order to mark a question as solved, then answers are given, correct? The other person gave an answer, as did Mario. Mario; decided on a wiki, because that was his choice. Mario has enough rep points and has already proven his competence a long time ago. Give the other guy a break. If it will add to his/her credibility and integrity as a community member, then that person stands at being trusted when giving answers, which hopefully will lead to a solution and that's what it's all about, finding solutions. –  Fred -ii- Jul 5 at 2:52
public function writeFormMessage($field, $message)
{
    $formMessages[$field] = $message;
}

public function getFormMessages()
{
    return $this->formMessages;
}

You are saying different things here, that's why you got empty from the result. You think you are refering to the same var, but you are not. $formMessages is a variable that exists only inside the WriteFormMessage function while $this->formMessages exists outside it.

Then you have to reference it with $this to get proper results.

    $this->formMessages[$field] = $message;
share|improve this answer
    
For goodness sake delete this post. –  Jeremy Jul 5 at 2:37
    
Thanks 1nflktd and Mario/Jeremy, appreciated. The clarification helped greatly. –  Error-404 Jul 5 at 2:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.