0

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!

10
  • ...what's your question? What part doesn't work? Your question is unclear. Commented Jul 5, 2014 at 2:26
  • What's the output of var_dump($test); ? Commented Jul 5, 2014 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. Commented Jul 5, 2014 at 2:28
  • formErrors isn't even in your posted code. Show us what/how you're using this. Commented Jul 5, 2014 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) Commented Jul 5, 2014 at 2:32

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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 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- This one was posted 6 minutes ago, the below 5 minutes.
@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.
@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.
|
1
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;

1 Comment

Thanks 1nflktd and Mario/Jeremy, appreciated. The clarification helped greatly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.