1

I am new to Laravel framework(i.e a framework driven from Symfony library) .I was looking at the code of Symfony and came across some short cuts that i think i can use to improve my ability to code neatly.

I want to know about the behavior of these operators? ( ?: etc)

  1-  $this->history = $history ?: new History();

//Does this mean create object of History class and store in $this->history?

  2-  $this->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects;

  3-   $this->followRedirects = -1 != $this->maxRedirects;

//Not sure how operators are behaving?i know this is something to do with regex but want to know the logic.

I would appreciate if somebody could post a link of tutorial about regex programming in php like above.

Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
9 Digit
  • 167
  • 2
  • 11

5 Answers5

2

The first and the second use the ternary operator:

condition ? code_if_true : code_if_false

Note that code_if_true or code_if_false can be empty.

the third assigns to the variable the result of the test: -1 != $this->maxRedirects

So, true or false.

1) $this->history = $history ?: new History();

if $history has a value equivalent to false the history attribute is set to a new instance of the class History. If $history has a value equivalent to true, the history attribute is set to the $history value.

2) $this->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects;

if $maxRedirects is negative the maxRedirects attribute is set to -1, otherwise it is set to $maxRedirects.

3) $this->followRedirects = -1 != $this->maxRedirects;

if $this->maxRedirects is different from -1, $this->followRedirects is set to true, otherwise false.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
1

1) and 3) are known as a ternary. I.e.

echo $isFoo ? "Is foo" : "No foo for you!";

Will echo the "Is foo" if foo is true and "No foo for you!" otherwise.

Since PHP 5.3 you can omit the middle:

echo $fooLabel ?: "Default foo label";

This will show $fooLabel if it is true and "Default foo label" otherwise.

Finally 3)

$this->followRedirects = -1 != $this->maxRedirects;

This simply evaluates -1 != $this->maxRedirects. This will be true if maxRedirects is not equal to -1. The result is then stored in $this->followRedirects.

Jim
  • 22,354
  • 6
  • 52
  • 80
1

The best way to understand it is to look at only one page:

http://php.net/manual/en/language.operators.comparison.php

I think that to work with Symfony you need be more experienced developer. Try to learn PHP first, then you can try to learn how to work with frameworks written on PHP. Also I think that before you will start to learn frameworks you should read some books about Design Patterns.

Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
0

The if-else shorthand follows the following format:

 <condition> ? <condition is met> : <condition is not met>

This:

$age = 20;
echo $age >= 21 ? 'Have a beer' : 'Too young! No beer for you!';

Is the same as this:

   if($age >= 21){
      echo 'Have a beer';
   }else{
       echo 'Too Young! No beer for you!';
   }

Your example #1, since PHP 5.3, simply omits the first condition of the shorthand and executes new History() only if the condition is not met:

$this->history = $history ?: new History();

Note you can also omit the second condition if you'd like. Also, if the omitted portion of the shorthand is met, 1 is returned, since no other instruction is given.

AnchovyLegend
  • 12,139
  • 38
  • 147
  • 231
  • But this code ($this->history = $history ?: new History();) written by Symfony developer (Fabien Potencier) and works as part of Symfony – 9 Digit Oct 16 '14 at 15:37
  • $this->history = $history ?: new History(); $this->cookieJar = $cookieJar ?: new CookieJar(); – 9 Digit Oct 16 '14 at 15:39
  • It's valid, not invalid. You can omit either the true or false behaviour in a ternary operator. – iamgory Oct 16 '14 at 15:40
0

I thinks it's best to illustrate how these operators work by example:

1)

$this->history = $history ?: new History();

Is equal to

if ($history) {
  $this->history = $history;
} else {
  $this->history = new History();
}

2)

$this->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects;

Is equal to

if ($maxRedirects < 0) {
  $this->maxRedirects = -1;
} else {
  $this->maxRedirects = $maxRedirects;
}

3)

$this->followRedirects = -1 != $this->maxRedirects;

Is equal to

$this->followRedirects = (-1 != $this->maxRedirects);

Is equal to

if (-1 != $this->maxRedirects) {
  $this->followRedirects = true;
} else {
  $this->followRedirects = false;
}
Fieg
  • 3,046
  • 1
  • 16
  • 22