0

It is possible that the two code below gives different results?

This runs as i expected:

$message = new Message();
$cond = $message->getMessage();
$helper->log($cond);
if(!empty($cond)){
  // do my stuff
}

This is where i start wondering why gave me different result:

$message = new Message();
$helper->log($message->getMessage());
if(!empty($message->getMessage())){
  // do my stuff
}

All i modified is that i changed $cond variable to $message->getMessage().

This is the constrictor of the Message Class:

class Message {
  private $input;
  private $message;

public function Message()
{
  $this->input = json_decode(file_get_contents('php://input'), true);
  // ...
  $this->message = $this->input['entry'][0]['messaging'][0]['message']['text'];
  //...
}
}

And this is the getter method:

public function getMessage()
{
  return $this->message;
}
5
  • What is the "different result" that you saw in the second case? Commented Sep 6, 2016 at 12:04
  • With same circumstances first code run into the if statement, but second doesn't. And the same is the behavior in the log function too. Commented Sep 6, 2016 at 12:06
  • 2
    Is getMessage() idempotent? Commented Sep 6, 2016 at 12:07
  • So the only difference is that in first code getMessage method called once. Does this method changes the state of $message object ? I would look at realization of getMessage method. Commented Sep 6, 2016 at 12:11
  • I updated my question with the relevant part of the constructor and the getter method. Commented Sep 6, 2016 at 12:16

1 Answer 1

4

Please check your php_error_log.

PHP's empty does not support expressions when you're using PHP version < 5.5:

Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. [...]

Possibly this is the resaon why the code inside your if statement isn't reached.

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

2 Comments

Thanks for your answer, with $message->getMessage() != "" it's working corrertly.
If this is your problem, you should consider upgrading your PHP version, because it should work in PHP 5.5, which is the lowest currently supported version. If you're on a version older than 5.5, you should be urgently considering upgrading.

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.