Skip to content

Instantly share code, notes, and snippets.

View bertramakers's full-sized avatar
🌱
Herbivore

Bert Ramakers bertramakers

🌱
Herbivore
  • publiq
  • Leuven, Belgium
View GitHub Profile
@bertramakers
bertramakers / benchmark-var-arguments.php
Last active March 22, 2017 16:44
Quick benchmarking of arrays vs variable number of arguments
<?php
function arrayFunction(array $numbers) {
print 'Got array with ' . count($numbers) . ' members.<br />';
}
function variableArgumentFunction(...$numbers) {
print 'Got array with ' . count($numbers) . ' members.<br />';
}
<?php
class Ratings extends GenericCollection
{
public function __construct(Rating ...$ratings) {
$this->values = $ratings;
}
public function getAverage() : Rating { /* ... */ }
}
<?php
abstract class GenericCollection implements IteratorAggregate
{
protected $values;
public function toArray() : array {
return $this->values;
}
<?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.
<?php
class Ratings implements IteratorAggregate {
private $ratings;
public function __construct(Rating ...$ratings) {
$this->ratings = $ratings;
}
public function getAverage() : Rating {
<?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!');
<?php
class Movie {
private $airdates;
private $ratings;
public function __construct(AirDates $airdates, Ratings $ratings) {
$this->airdates = $airdates;
$this->ratings = $ratings;
}
<?php
$movie = new Movie();
$movie->setAirDates(
\DateTimeImmutable::createFromFormat('Y-m-d', '2017-01-28'),
\DateTimeImmutable::createFromFormat('Y-m-d', '2017-02-22')
);
<?php
class AirDates implements IteratorAggregate {
private $dates;
public function __construct(\DateTimeImmutable ...$dates) {
$this->dates = $dates;
}
public function getIterator() {
<?php
declare(strict_types=1);
class Ratings implements IteratorAggregate {
private $ratings;
public function __construct(float ...$ratings) {
$this->ratings = $ratings;
}