Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Marco Perone:
Maybe in PHP
Jun 22, 2017 @ 10:35:16

In a recent post to his site Marco Perone looks at the idea of "maybe" in PHP, having functionality that acts a default value if no value is present. This idea is implemented in other languages like Haskell and Elm.

Doing functional programming in a language as PHP, which is almost completely used as an imperative or object oriented way, is not always easy. Good progresses have been made thanks to the introduction of callable type hints in PHP 5.4 and the diffusion of functional interfaces like the ones present in PSR-7.

Still, all “good” PHP code is still written using objects and classes and the object oriented perspective on the world strongly influences even the most functional oriented libraries.

In this post I would like to propose as an example how we could implement the Maybe type in PHP. We will see how some open source libraries do this, we will see an alternative solution and we will raise concerns about some modelling issues.

He starts off by describing what the "maybe" functionality is and gives some examples of it in use in other languages. He points out that while there's several PHP libraries that implement this kind of default handling, it's not in the PHP core language. He works through some of these libraries and shows them in use: monad-php, Phunkie, php-maybe-monad and php-fp-maybe. He wraps up the post showing his own suggested implementation and how it could help resolve some of the issues he found with the other libraries as he worked through them.

tagged: maybe language default variable functional tutorial library

Link: http://marcosh.github.io/post/2017/06/16/maybe-in-php.html

SitePoint PHP Blog:
Functional Programming with Phunkie: Building a PHP JSON Parser
Jun 09, 2017 @ 12:12:29

The SitePoint PHP blog has a new tutorial posted from author Marcello Duarte showing you how to use functional programming techniques in your applications with the help of the Phunkie library. Phunkie is a library that brings functional programming functionality to PHP. This article was originally published on the Inviqa blog.

In the first part of this series we explored parsers and combinators to help you start getting values from functional programming with PHP. We covered the basics using examples, and now we’ll move onto more sequencing and other strategies.

Continuing on, they work towards the goal of making a more useful end result, a JSON parser that returns a JsonObject. First, though, he goes through several different combinators, showing code examples for each: sequencing, choice, and recursive. He also covers the repetition pattern and how to integrate separators. Finally, with this groundwork laid, he gets to the JSON parser showing each step of the way, from reading in the JSON string to returning the object.

tagged: functional programming phunkie json parser combinator tutorial

Link: https://www.sitepoint.com/functional-programming-phunkie-building-php-json-parser/

SitePoint PHP Blog:
Functional Programming with Phunkie: Parser Combinators in PHP
May 03, 2017 @ 10:53:08

The SitePoint PHP blog has posted a tutorial by Marcello Duarte showing you how to use functional programming in PHP with the help of the Phunkie library.

Phunkie is a library with functional structures for PHP. In this tutorial, Phunkie creator Marcello Duarte, head of training at Inviqa, explains how to create Parser combinators using the functional library.

[...] Learning functional programming means immersing yourself in a new paradigm, which can be challenging. It requires a totally different mindset for approaching problems. So, by the time you can use functional programming to solve real-world problems, you’ll have spent hours grasping the new thinking or getting clued-up on the theory.

In this tutorial, my aim is to help fast-track your journey by going over my implementation of Hutton & Meijer’s Monadic Parser Combinators.

He starts off by defining two terms that will be used through out the code: parsers and combinators. He shares a code example of a combinator and then moves on to examples of primitive parsers and parser combinators. Each section includes the code you'll need to use (making use of Phunkie) to make the functional magic happen.

tagged: functional programming phunkie tutorial parser combinator

Link: https://www.sitepoint.com/functional-programming-with-phunkie-parser-combinators-in-php/

Three Devs & A Maybe:
Aha! Moments with Steven Proctor
Feb 13, 2017 @ 12:38:01

The Three Devs and a Maybe podcast, with hosts Michael Budd, Fraser Hart, Lewis Cains and Edd Mann, has posted their latest episode - Aha! Moments with Steven Proctor.

In this weeks episode we are lucky to have Steven Proctor back on the show. We start off discussion by congratulating him on 82 episodes of Functional Geekery, and the commitment it takes to do a podcast and not just ‘podfade’. From here we move on to highlight any commonalities he notices with people getting into FP, how he stays on-top of the latest advancements and how he finds the guests he wishes to speak to. This leads us on to compare learning functional concepts within a language you already know vs. in a totally different language which is rooted in the principles. Finally, we chat about interesting projects that are on his radar and advice that he has for people who wish to begin exploring FP.

You can listen to this latest episode either using the in-page audio player or by downloading it directly. If you enjoy the show, be sure to subscribe to their feed and follow them on Twitter for updates when new shows are released.

tagged: threedevsandamaybe podcast stevenproctor aha functional programming

Link: http://threedevsandamaybe.com/aha-moments-with-steven-proctor/

DeliciousBrains.com:
Writing Functional Tests for WP-CLI Packages
Jan 05, 2017 @ 12:57:25

On the Delicious Brains blog there's a post sharing some of their knowledge about building tests for WP-CLI packages, a set of command line tools for administering a WordPress installation. Their testing makes use of the Behat testing tool (already in use on WP-CLI's own tests).

My last article was part of a short series on automating local WordPress site setup. In that series, we created a WP-CLI package that helps with installing and uninstalling WordPress development environments, and we even got it submitted to the WP-CLI Package Index.

[...] In this post we’re going to take a bit of a break from automating WordPress installs and start writing some functional tests to make sure that everything works as expected. While I’ll be writing the tests for the wp installer command, the same concepts should apply for any WP-CLI package.

They start by clarifying the difference between functional and unit tests and how to get your environment all set up and ready to use for testing. They help you get the wp_scaffold_package installed and how to confirm that everything is working as expected. From there it's all about the tests: ensuring that a package is active, creating a custom step to use in testing and an example of what the output should look like.

tagged: functional test wordpress wpcli package behat tutorial

Link: https://deliciousbrains.com/writing-functional-tests-wp-cli-packages/

QaFoo Blog:
Introduction To Page Objects
Sep 06, 2016 @ 11:03:17

The QaFoo blog has a post to their blog introducing page objects and how they're useful in functional testing to help provide a "decoupling" from the actual frontend.

A while ago we wrote about writing acceptance tests (end-to-end tests) with Mink and PHPUnit. While this is a great set of tools for various applications such tests tend be susceptible to changes in the frontend. And the way they break is often hard to debug, too. Today I will introduce you to Page Objects which can solve some of these problems.

The basic idea behind a Page Object is that you get an object oriented representation of your website. The Page Objects maps the HTML (or JSON) to an object oriented structure you can interact with and assert on. This is more initial work then than writing tests with PHPUnit and Mink directly, but it can be worth the effort.

They use the Mnk testing tool to simulate a browser and some previously shared functionality to lay the foundation. From there they write up a first test using a "Login" page object and processing the username/password handling of the page. They show how to implement a custom page object with a bit of additional logic and put it to use in processing the request. They also include an update when, for example, a site is switched from Twig templates to a React.js component and how the Page object would need to be refactored for the update.

tagged: page object functional test mink behat example tutorial

Link: https://qafoo.com/blog/089_introduction_to_page_objects.html

Vic Cherubini:
Writing Functional Tests for Services in Symfony
Jun 16, 2016 @ 12:35:07

Vic Cherubini has written up a tutorial on his site showing you how to write functional tests for Symfony services in your application. He provides a practical example of testing a basic Symfony service and the configuration/code to go with it.

The dependency injector is an amazingly simple and flexible addition to Symfony, and one you should be using to properly structure your application. But what happens when you want to write a functional (or integration) test for a service that depends on another service? This article will show you an easy way to test complex services.

He sets up a simple InvoiceGenerator service that takes in a Doctrine entity manager and a "payment processor" instance. He stubs out a simple PaymentProcessor class and shows the configuration needed to set it all up for correct injection. He then gets into the testing of this setup, creating a simple test case that requests the invoice generator from the service container. In this call the services_test definition overrides the default and injects the test payment processor instead of the actual one.

tagged: symfony functional test services example tutorial configuration container injection

Link: https://viccherubini.com/2016/06/writing-functional-tests-for-services-in-symfony

Toptal.com:
Jumpstart Your PHP Testing with Codeception
May 26, 2016 @ 12:41:35

The Toptal.com blog has posted a new tutorial that wants to help you make the most of your application via testing. They show you how to use Codeception to create a set of tests to ensure your application is working as expected.

Before moving on to Codeception and PHP, we should cover the basics and start by explaining why we need testing in applications in the first place. Perhaps we could complete a project without wasting time on tests, at least this time?

Sure, you don’t need tests for everything; for example, when you want to build yet another homepage. [...] However, you definitely do need testing when: your team uses BDD/TDD, your Git repo contains more than a couple commits, [and] you are a proper professional, working on a serious project.

They start with a look at the kinds of things testing solves in your development process and the different kinds of tests you can create. From there they introduce Codeception, an alternative testing tool to the widely used PHPUnit. The tutorial helps you get it installed and shows you how to make a simple, first test. It helps you execute the test, debug issues that might pop up and the different assertions you can use. With the fundamentals in place, they move on to more details on using it for functional and unit testing.

tagged: jumpstart testing codeception tutorial functional unit

Link: https://www.toptal.com/php/php-testing-with-codeception

SitePoint PHP Blog:
Transducers in PHP Made Easy
Apr 19, 2016 @ 11:16:25

On the SitePoint PHP blog they've posted a tutorial showing you how to work with transducers in PHP. Transducers are pieces of functionality that allow you to transform data in a reusable way.

Have you heard of functional programming, high order functions, etc. before? Probably, right? However, when you hear “transducers”, do you know what those are? [...] A reducing function is just the kind of function you’d pass to reduce – it takes a result so far and a new input and returns the next result-so-far. A transducer is a function that takes one reducing function and returns another.

Transducers were first introduced into Clojure by Rich Hickey, and ported to PHP by Michael Dowling. Transducers are a powerful way to build algorithmic transformations that you can reuse in many contexts. In this article, we’re going to take a look at how they could be useful through a set of practical examples.

They help you get the mtdowling/transducers library installed via Composer and include a simple example using a User instance and uppercasing the first letter of the user's name. Other examples of the transducer functionality are also included such as: converting values to strings, filtering and composing sets of multiple transformations. The tutorial also shows you how to extend the current functionality and create your own transducer class (their example drops null values).

tagged: transducer functional programming reusable tutorial transform

Link: http://www.sitepoint.com/transducers-in-php-explained-and-demonstrated/

Mark Baker:
A Functional Guide to Cat Herding with PHP Generators
Jan 19, 2016 @ 10:05:13

In this post to his blog Mark Baker looks at a feature added in PHP 5.5 - generators - and how to use them with some of the array handling functionality PHP provides.

When working with arrays in PHP, three of the most useful functions available to us are array_map(), array_filter() and array_reduce().

[...] However, these functions only work with standard PHP arrays; so if we are using Generators as a data source instead of an array, then we can’t take advantage of the functionality that they provide. Fortunately, it’s very easy to emulate that functionality and apply it to Generators (and also to other Traversable objects like SPL Iterators), giving us access to all of the flexibility and power that mapping, filtering and reducing can offer.

He starts with a more "real world" example of using a generator in a handler for GPX files, XML files storing GPS data. He gives an example of the typical file contents and shows a simple generator script (class) that he uses to grab chunks of the file at a time instead of reading it all in and parsing it from there. He then uses this generator along with a bit of extra handling to mimic array filtering, transformation and reducing the data being returned.

tagged: functional generator tutorial array filter reduce transformation

Link: http://markbakeruk.net/2016/01/19/a-functional-guide-to-cat-herding-with-php-generators/