I'm Mészáros Róbert – a full-stack developer who enjoys working in cross-functional teams the most.

Currently, together with a WordPress VIP agency, we support enterprise clients.

At Codeable, I have mainly vetted experts lately.

implenton is the name of this development-related space.

Recent posts

There is the ... operator in PHP (and other languages) known as the splat operator, which, when combined with type hinting, proves to be extremely useful for accepting any number of "things."

However, what if you want to ensure at least one "thing"?

If you do the checking "manually", that's totally fine. Depending on your use case, you can deal with no "things" or require it.

class Foo
{
private array $conditions = [];
public function __construct(Condition ...$conditions)
{
// Let's make damn sure there's a condition!
assert(!empty($conditions));
$this->conditions = $conditions;
}
public function __invoke(): array
{
// Or be less strict about it and handle it as a possible state
if (empty($this->conditions)) {
return [];
}
// ...
}
}

The alternative solution looks like this:

class Foo
{
private array $conditions = [];
public function __construct(Condition $firstCondition, Condition ...$conditions)
{
$this->conditions = [$firstCondition, ...$conditions];
}
public function __invoke(): array
{
// No checks needed
// ...
}
}

I like the expressivity of this solution, and it's also more compact.

Since the splat operator has been around for ages, I can even imagine that this "pattern", "trick" is called somehow.

Now, it's just a matter of remembering and applying. That's the "easy" part.

If we exaggerate a little, there's always something new to learn when reading someone else's code, even for seasoned veterans like Freek Van der Herten.

In his article, Discovering PHP's first-class callable syntax, Freek shares how he came across a PHP feature while examining the Laravel codebase.

This is the first example I've come across where this particular feature is employed in such an appealing way.

Featured Publications