This file contains hidden or 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 | |
| function arrayFunction(array $numbers) { | |
| print 'Got array with ' . count($numbers) . ' members.<br />'; | |
| } | |
| function variableArgumentFunction(...$numbers) { | |
| print 'Got array with ' . count($numbers) . ' members.<br />'; | |
| } |
This file contains hidden or 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 Ratings extends GenericCollection | |
| { | |
| public function __construct(Rating ...$ratings) { | |
| $this->values = $ratings; | |
| } | |
| public function getAverage() : Rating { /* ... */ } | |
| } |
This file contains hidden or 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 | |
| abstract class GenericCollection implements IteratorAggregate | |
| { | |
| protected $values; | |
| public function toArray() : array { | |
| return $this->values; | |
| } | |
This file contains hidden or 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 | |
| // Initial Ratings collection | |
| $ratings = new Ratings( | |
| new Rating(1.5), | |
| new Rating(3.5), | |
| new Rating(2.5) | |
| ); | |
| // Convert the collection to an array. |
This file contains hidden or 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 Ratings implements IteratorAggregate { | |
| private $ratings; | |
| public function __construct(Rating ...$ratings) { | |
| $this->ratings = $ratings; | |
| } | |
| public function getAverage() : Rating { |
This file contains hidden or 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 | |
| declare(strict_types=1); | |
| class Rating { | |
| private $value; | |
| public function __construct(float $value) { | |
| if ($value < 0 || $value > 5) { | |
| throw new \InvalidArgumentException('A rating should always be a number between 0 and 5!'); |
This file contains hidden or 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 Movie { | |
| private $airdates; | |
| private $ratings; | |
| public function __construct(AirDates $airdates, Ratings $ratings) { | |
| $this->airdates = $airdates; | |
| $this->ratings = $ratings; | |
| } |
This file contains hidden or 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 | |
| $movie = new Movie(); | |
| $movie->setAirDates( | |
| \DateTimeImmutable::createFromFormat('Y-m-d', '2017-01-28'), | |
| \DateTimeImmutable::createFromFormat('Y-m-d', '2017-02-22') | |
| ); |
This file contains hidden or 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 AirDates implements IteratorAggregate { | |
| private $dates; | |
| public function __construct(\DateTimeImmutable ...$dates) { | |
| $this->dates = $dates; | |
| } | |
| public function getIterator() { |
This file contains hidden or 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 | |
| declare(strict_types=1); | |
| class Ratings implements IteratorAggregate { | |
| private $ratings; | |
| public function __construct(float ...$ratings) { | |
| $this->ratings = $ratings; | |
| } |
NewerOlder