Usage of the Conditional Ternary operator to reduce brace and newline waste when processing optional method parameters

ralphschindler writes; Usage of the Conditional Ternary operator to reduce brace and newline waste when processing optional method parameters

< ?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;
}
}


via Gist.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.