Created
March 4, 2011 15:59
-
-
Save ralphschindler/854866 to your computer and use it in GitHub Desktop.
Usage of the Conditional Ternary operator to reduce brace and newline waste when processing optional method parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Coordinate | |
{ | |
protected $x; | |
protected $y; | |
public function __construct($x = null, $y = null) | |
{ | |
(empty($x)) ?: $this->setX($x); | |
(empty($y)) ?: $this->setY($y); | |
} | |
/* What we're trying to replace | |
public function __construct($x = null, $y = null) | |
{ | |
if ($x) { | |
$this->setX($x); | |
} | |
if ($y) { | |
$this->setY($y); | |
} | |
} | |
*/ | |
public function setX($x) | |
{ | |
$this->x = $x; | |
} | |
public function setY($y) | |
{ | |
$this->y = $y; | |
} | |
} |
It's not. I think our coding standard has always shunned brace-less if statements.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How is this any better/worse than: