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

The last time I posted here, I was writing about Anonymous Functions and how they can be bound to any object (or class) to execute as though they are a method within the scope of that class (Closure Binding as an alternative to “use” variables); and in the first article in this series, I looked at using a Closure to access private and protected properties of an object.

I was going to write this particular article about using simple Anonymous Classes to create test doubles for Unit Testing – and may well return to that topic in a future article in the series – but Matt Brunt has written a good post on that topic already, so instead I’m going to focus on a different approach to using an Anonymous Class to verify the values of object properties that we otherwise couldn’t see directly when testing a class.

Continue reading

Posted in PHP | Tagged , , , , , | Leave a comment

Closure Binding as an alternative to “use” variables

As a general rule when creating a Closure, arguments are passed when the function is called, but “use” variables (I’m sure that they have a formal name, but have no idea what it might be, so I just refer to them as “use” variables because they’re passed to the Closure through a “use” clause) are fixed as the value that they already contain when the Closure is defined, and the variables themselves must already exist within that scope. This isn’t normally a problem where we’re defining the Anonymous function inline, because we can specify the values directly in the callback function itself:


$filteredArrayData = array_filter(
    $arrayData,
    function($value) { return $value->price >= 120.00 && $value->price < 250.00; }
);

Writing our callbacks like this has the big benefit of being easy to read and understand exactly what the callback is doing.

Of course, the drawback of this approach is that when we need to change the price minimum and maximum values for filtering, they’re hard-coded in the callback.
For those array functions that use callbacks, such as array_filter(), we can’t simply pass the values as extra parameters directly to the function; although we can define the price range values as variables that can then be passed to the callback function as “use” variables. Perhaps a more practical approach than hard-coding them if we get the minimum and maximum values for filtering from user input, or if they need to be calculated elsewhere in our code.


$minimumPrice = 120.00;
$maximumPrice = 250.00;

$filteredArrayData = array_filter(
    $arrayData,
    function($value) use ($minimumPrice, $maximumPrice) {
        return $value->price >= $minimumPrice && $value->price < $maximumPrice;
    }
);

Continue reading

Posted in PHP | Tagged , , , | 14 Comments

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(
    $arrayData,
    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 , , , , | 4 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’M REAL! I’M REAL!

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