user991987
user991987

Reputation: 213

Ternary if statement involving php class

Can someone please explain to me the following line of php?

($myobjectfunction = object::function('letsgo')) ? eval($myobjectfunction) : false;

I understand objects and their functions. Is this php saying if $myobjectfunction is defined then eval $myobjectfunction, otherwise do nothing? Because in the code I am reading, object hasn't yet been defined before this line (sometimes).

Upvotes: 0

Views: 494

Answers (4)

Michael Berkowski
Michael Berkowski

Reputation: 270677

This line assigns the returned value from the function object::function('letsgo') to the variable $myobjectfunction. If the return was a "truthy" value (evaluates to boolean TRUE), the contents of that variable are then evaluated as PHP code (eval). If the initial return was FALSE, no further action is taken. The false at the end basically does nothing.

This works because PHP will return the value from an assignment, even though it isn't usually used for anything. In the case of your bit of code, however, the return from the assignment is used to determine which branch of the ternary operator to take since it is enclosed in parentheses.

$x = 3;
// returns 3 even if we don't use it...

This is an unusual idiom, because the parentheses are around the initial assignment.

   ($myobjectfunction = object::function('letsgo')) ? eval($myobjectfunction) : false;
//^^---------------------------------------------^^^

A more typical usage of the ternary operator would assign the output of either side of the ? to the variable on the left, based on the condition to the right of the = like:

$myobjectfunction = object::function('letsgo') ? $someothervalue : false;

Upvotes: 5

Jordan Running
Jordan Running

Reputation: 106077

This code is equivalent to:

$myobjectfunction = object::function('letsgo');

if( $myobjectfunction ) {
  eval( $myobjectfunction );
}
else {
  false;
}

In other words, assign the result of object::function( 'letsgo' ) to a variable. If that variable is "truthy" (i.e. not false, null, or 0, or another value that evaluates like false) then eval its contents; otherwise do nothing.

Upvotes: 1

Peter O'Callaghan
Peter O'Callaghan

Reputation: 6186

It's difficult to tell exactly what's going on here. I'm assuming you have substituted actual values in order to 'simplify' the example, but the use of keywords actually clouds the matter.

The declaration of the class 'object' doesn't need to be before this statement, so long as the object class is defined at some point during the code execution.

Upvotes: 1

RedactedProfile
RedactedProfile

Reputation: 2808

ya you pretty much got it, well its saying IF $myobjectfunction has successfully been returned a positive result (ie: not false, 0, or null) the eval the new variable object, but i probably wouldnt use "false" in the else bit., id probaby use null.

Now for this to do anything, "object" does need to be defined

this is a strange piece of code though, in my own honest opinion

Upvotes: 0

Related Questions