Closures, Anonymous Classes and an alternative approach to Test Mocking (Part 1)

Since their first introduction with PHP 5.3, Closures have proven an incredibly useful feature for writing simple callback code, making it cleaner and more intuitive. Anonymous Functions can be used inline for many of the array functions

$price = 100.00;
$filteredArrayData = array_filter(
    function($value) use ($price) { return $value->price >= $price; }

or assigned to a variable as a Lambda that can be referenced many times in different places in your code.

$sortByField = function($valueA, $valueB) use (&$field) {
    return $valueA->$field <=> $valueB->$field;

$field = 'title';
usort($arrayData, $sortByField);

But this isn’t an article about the differences between Anonymous and Lambda Functions and Closures, or about whether Closures really are Closures compared to their equivalent in other languages, or about the difference between the function arguments and “use” variables, or even the significance of “pass by reference” in this example; there are other blogs that cover such topics. Instead, I want to take a look at binding Closures to objects as a first step to demonstrating an alternative approach to test mocking.

Continue reading

Posted in PHP | Tagged , , , , | 6 Comments

Heroes of PHP™ #3

Last year at Christmas I posted my original 24 Heroes of PHP™, with a second post listing a further 24 of my heroes at New Year. I’ve decided to repeat the exercise this year, although my list this time is just 11 names. All of these are people that have inspired me in some way or other in 2016.
Continue reading

Posted in PHP | 1 Comment

Musings of a PHPDiversity Rainbow Elephpant

IT’S HAPPENING! Finally, the kickstarter is happening! And I’m ecstatic.

I’d so been hoping that the kickstarter would be launched last Friday, and it felt frustrating when the day ended, and I hadn’t heard anything back from the kickstarter review team. And then I was twiddling my toes all weekend because I knew that I couldn’t do anything until the next working week; but then Monday arrived, and the kickstarter was rejected because of its potential charitable aspects. It was such a disappointment!

But Mark has since reworked it, ensuring that it conforms to all the kickstarter guidelines, and submitted it to appeal; and now I’ve had the good news from kickstarter, and the launch is happening… and I’m so happy again, because it’s the next step in my plans for world domination spreading the Diversity message throughout the worldwide PHP Community.

Promoting Diversity within the worldwide PHP Development Community regardless of race, gender, sexual orientation, gender identity, age, nationality, disability, religion and technology.

So the kickstarter campaign is about to go live, and will run until shortly before midnight on December 8th (UTC) and I’m a very happy Elephpant. Here’s the kickstarter link: please help make me a success.

Continue reading

Posted in PHP | Tagged , , | Leave a comment

Musings of a PHPDiversity Rainbow Elephpant


I know that I shouldn’t shout; but I just want to tell the whole world that I’M REAL! I’M HAPPENING! I’M A REAL ELEPHPANT!

When Mark got home from work this evening, he found me sitting in my travelling box, just waiting for him to arrive.

The real Enfys comes face-to-face with her Inspiration

The picture doesn’t really do me justice, the lighting makes me seem darker than I really am (I’m really a bright and cheerful soul), so I’ll sort out some better pictures of me in natural light tomorrow (or over the coming weekend). When I do, you’ll see that I’m a little different to those original photos of me from back in June: the factory clearly took note of Mark’s comments back then; and (even though I do say so myself) I look even better than he’d ever imagine that I would. And (of course) this final prototype me has my PHP logo and my toes.

But the final arrival of the prototype me is just the latest thing to happen. It’s been a very busy week, with all manner of exciting things happening, as the launch of my kickstarter project approaches at a rush.

Continue reading

Posted in PHP | Tagged , , | 1 Comment

PHP Generators – Sending “Gotchas”

If you’re reading this, you’re probably already aware of just how useful PHP’s Generators are for improving performance and/or reducing memory overheads while keeping your code clean and easy to read. We can use a simple foreach() in the main body of our code, as though iterating over an array, but without the overheads of needing to actually build an array. The Generator itself handles a lot of the boilerplate that we’d otherwise have to write, which also means that we create simpler, cleaner code.

Unlike their equivalent in some programming languages, PHP’s Generators allow you to send data into the Generator itself; not simply at initialisation (the arguments that we pass to the Generator when instantiating it); but also between iterations. This has its own uses, and again, allows us to move code from our main blocks and methods into the Generator itself.

Less commonly recognised is that we can combine these two features, creating Generators that can be used to provide data for our main code block and methods, while allowing us to send data to the Generator that can actually modify its behaviour dependent on circumstance.

However, there are a few “gotchas” when we combine Generators that both return and accept data in this way, and it really helps to be aware of them when we’re developing, otherwise it can create problems.

Continue reading

Posted in PHP | Tagged , | 4 Comments

Musings of a PHPDiversity Rainbow Elephpant

It’s been a long, slow, lazy summer; but it feels as though things are accelerating in my life at the moment as we approach the launch of the PHPDiversity kickstarter. Mark promised himself that he wouldn’t proceed until he had all the costs associated with making me a reality (and knew that I was financially viable), and until he’d held the prototype me in his hands (rather than simply seen photographs of me) to assess that I really was what he’d expected.

Well, he’s been able to put those final figures into his spreadsheets now; and the prototype me is on its way to him even as I type. Assuming that it matches his expectations, he reckons that the kickstarter to make me real will launch toward the end of this month (October) or early November at the latest.

Continue reading

Posted in PHP | Tagged , , | 1 Comment

Words on Diversity from the PHP Rainbow Elephpant

Enfys, the PHP Rainbow Elephpant, is intended to help promote Diversity within the PHP Community: but what exactly is Diversity? What are the benefits of Diversity? And how can Diversity be achieved?

Continue reading

Posted in PHP | Tagged , , | Leave a comment