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

Nicola Malizia:
Understanding Laravel’s Automatic Facades
Mar 27, 2017 @ 09:24:36

On his Unnikked site Nicola Malizia has written up a post sharing some insight into how Laravel's automatic facades work, a feature that was introduced in Laravel v5.4.

Another cool feature shipped with the release of Laravel 5.4 is the ability to use Facades on the fly. Automatic Facades with the official documentation is quite informative. So I assume you are familiar with them.

He then starts in on the creation of custom facades: defining the functionality (a class), creating the facade and updating the app config to load it in. He then covers what's happening behind the scenes when the "Facades" namespace is loaded and the facade is called (lots of code snippets included here).

My suggestion is to start using automatic facades whenever is possible, their impact is negligible. To see when to use facades in general a good read is available in the official documentation.
tagged: laravel automatic facade tutorial introduction

Link: https://unnikked.ga/understanding-laravels-automatic-facades-d623e4774e5f#.hi2vq6igs

Benjamin Eberlei:
Explicit Global State with Context Objects
Mar 24, 2017 @ 11:50:12

In a post to his site Benjamin Eberlei looks at global state in PHP using something called "context objects" and how they can be used as an alternative to true global state.

Global State is considered bad for maintainability of software. Side effects on global state can cause a very nasty class of bugs. Context objects are one flavour of global state. For example, I remember that Symfony1 had a particularly nasty context object that was a global singleton containing references to very many services of the framework.

As with every concept in programming, there are no absolute truths though and there are many use-cases where context objects make sense. This blog posts tries to explain my reasons for using context objects.

He starts by getting everyone on the same page by defining a context - the "circumstances in which something can be fully understood". He then moves into the world of context objects, talking about how they encapsulate the information other objects need to execute. They're essentially "container" objects that allow for more control that something like the normal PHP superglobals. From there he helps you define what kind of context objects you might need in your application and provides a real-world example from his own experience at Tideways.

tagged: global state context object tutorial introduction definition

Link: https://beberlei.de/2017/03/12/explicit_global_state_with_context_objects.html

SitePoint PHP Blog:
Parallel Programming with Pthreads in PHP – the Fundamentals
Mar 24, 2017 @ 10:40:07

The SitePoint PHP blog has posted a tutorial that introduces some of the fundamentals of parallel programming in PHP. In their examples they make use of the pthreads extension to help bring simpler parallel programming to the language (otherwise you'd have to do odd things with shell commands and foreground/background controls).

PHP developers seem to rarely utilise parallelism. The appeal of the simplicity of synchronous, single-threaded programming certainly is high, but sometimes the usage of a little concurrency can bring some worthwhile performance improvements.

In this article, we will be taking a look at how threading can be achieved in PHP with the pthreads extension. This will require a ZTS (Zend Thread Safety) version of PHP 7.x installed, along with the pthreads v3 installed.

Despite the article being about the use of pthreads, it starts out talking about when not to use it, possibly saving you some time in the long run. With that out of the way it then starts in on the handling of "on-off tasks" with an example of fetching the "title" value from Google.com. This is then enhanced showing how to use the "Threaded" base class to define other classes that can be used inside of threads. The article moves on covering other topics including:

  • recycling threads
  • pthreads and (im)mutability
  • synchronization of threads

Each item in the list comes with plenty of example code showing you how to create the classes that execute the threads and the output they should generate.

tagged: parallel programming fundamentals tutorial introduction pthreads extension

Link: https://www.sitepoint.com/parallel-programming-pthreads-php-fundamentals/

Leonid Mamchenkov:
Getting started with workflows in PHP
Mar 21, 2017 @ 12:12:24

Leonid Mamchenkov has put together a post for those out there trying to get started with workflows in PHP and offers some tips from his own experience.

For a large project at work, we need to integrate or develop a workflow engine. I worked a little bit with workflow engines in the past, but the subject is way to big and complex for me to claim any expertise in it.

So, I am looking at what’s available these days and what are our options. This post is a collection of initial links and thoughts, and it’s goal is mostly to document my research process and findings, and not to provide any answers or solutions yet.

He starts off by defining the requirements of the system he's looking for, both on the technology side and the functional side. He outlines his expected flow and then links to some other resources he found to help define common terminology and some of the standards he found. He found the BPMN v2.0 and tracked down several PHP packages that implement that workflow structure (all linked as well).

tagged: workflow gettingstarted introduction concepts packages

Link: http://mamchenkov.net/wordpress/2017/03/20/getting-started-with-workflows-in-php/

Nicola Malizia:
Understanding Laravel’s HighOrder Collections
Mar 14, 2017 @ 09:11:59

Nicola Malizia has written up a tutorial that helps to explain Laravel's HighOrder collection functionality, a feature that was added in Laravel 5.4.

A new version of Laravel is available from 24 January 2017 and, as usual, it comes with a lot of new features.

Among them, there is one that takes advantage of the dynamic nature of PHP. Some out of there will contempt this, but I find it awesome!

He talks briefly about what the normal Collection class provides and provides an example of creating a collection and using the "map" function to return an average. With the new functionality the methods can be called directly on the collection with a simplified format. With the example out of the way he then dives into the source code for the feature, showing how it defines the "proxy" methods allowed and uses the __get and __call magic methods to map the method calls back to a collection.

tagged: laravel highorder collection tutorial introduction code

Link: https://unnikked.ga/understanding-laravels-highorder-collections-ee4f65a3029e#.uo1gmhbgu

How to Program With Yii2: ActiveRecord
Mar 09, 2017 @ 12:07:53

In the latest tutorial in their "Programming with Yii2" series the TutsPlus.com site shows you how to work with the ActiveRecord functionality included with the framework.

In this Programming With Yii2 series, I'm guiding readers in use of the Yii2 Framework for PHP. In today's tutorial, I'll walk you through using Yii's object-relational mapping, known as ORM, for working with databases. It's called Active Record and is a key aspect of programming database applications efficiently in Yii.

Yii offers different ways to work with your database programmatically, such as direct queries and a query builder, but using Active Record offers a complete set of benefits for object-oriented database programming.

The article goes on from there and defines some of the basics around what Active Record is and how it works. It then starts on the code, showing how to create an ActiveRecord model and execute queries to:

  • locate single or multiple records
  • build queries
  • counting records
  • and accessing the records returned

They also talk about mass assignment, saving data via model instances, deleting records and creating relationships between the models.

tagged: yii2 framework series activerecord database tutorial introduction

Link: https://code.tutsplus.com/tutorials/how-to-program-with-yii2-active-record--cms-27434

Delicious Brains:
Introducing WP Image Processing Queue - On-the-Fly Image Processing Done Right
Mar 09, 2017 @ 09:28:59

The Delicious Brains site has a new tutorial posted introducing WP Image Processing Queue, a tool that allows for on-the-fly image processing in your WordPress application via background processing.

I think the best solution is to get background processing into WordPress core so that all themes/plugins can share a single queue and ensure we don’t impact server performance. And so started my crusade.

At PressNomics, I had a great chat with Mike Schroder. He presented a very good path to core: find a feature that WordPress core needs and that needs background processing. In other words, piggyback! This is exactly how the image optimization stuff made it into core last year: by piggybacking off of responsive images. For background processing, he proposed coming up with an alternative to on-the-fly image processing (OTFIP). Whoa, turns out OTFIP is a problem we regularly deal with for WP Offload S3 as well. This could be a “two birds – one stone” kind of thing. Stars were aligning.

He talks more about some of the current discussions and efforts around processing the images like this (with OTFIP, On The Fly Image Processing). He covers some of the libraries that are currently out there for this processing and how, ultimately, the image processing queue came out to replace them as a result of some work at WordCamp US Contributor Day. He gives an example of the code needed to resize the images and the resulting markup. The post ends with the work he's planning on getting this queuing into the WordPress core and encourages plugin authors to use the OTFIP functionality rather than an external library.

tagged: wordpress image processing queue introduction onthefly

Link: https://deliciousbrains.com/introducing-wp-image-processing-queue/

Get Started Running Laravel in a Docker Container
Mar 07, 2017 @ 14:07:28

The Scotch.io blog has a new tutorial posted showing you how to get a Laravel application up and running in a Docker container with a minimal amount of work. The article assumes you already have a working knowledge of Laravel and Docker, so if you're not familiar with those you'll need to read up there first.

Laravel and Docker are two very popular tools of choice when considering building for the web. Although both of them do very different things, they can both be combined to create amazing products.

For our use case, we will be running Laravel in a Docker container. This is going to be a simple demonstration on how to use both products to create real-life applications. Nothing heavy.

The tutorial starts with some of the prerequisites you'll need before getting started but quickly gets into the commands you'll need to set up your environment. Using a custom Docker container they show you the configuration to get the container set up, how to build the database connection, bring the container up and verify all is working as expected.

tagged: docker laravel container tutorial introduction installation configuration

Link: https://scotch.io/tutorials/get-started-running-laravel-in-a-docker-container

Using Dependency Injection in PHP
Mar 03, 2017 @ 10:05:37

The PHPBuilder.com site has posted a tutorial talking about dependency injection in PHP applications covering not only the use of dependency injection (DI) containers but also constructor, setter, property and reflection based injection methods.

Dependency injection is a software design pattern that implements the inversion of a control concept for resolving dependencies. According to this concept, a class should not configure its dependencies statically, but should be configured from the outside.

A dependency is an object that can be used (a service) and an injection is the passing of a dependency to a dependent object (a client) that would use it. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.

The tutorial starts out with a simple example of a set of classes that depend on each other through the creation of internal objects. They then show how the different types of dependency injection can help reduce these dependencies with brief descriptions and sample code for each. The final example, a dependency injection container, gives a quick example but also links to several package options you could pull into your application including Pimple, Aura.Di and PHP-DI.

tagged: dependency injection tutorial introduction setter constructor container property

Link: http://www.phpbuilder.com/articles/application-architecture/design/using-dependency-injection-in-php.html

SitePoint PHP Blog:
Laravel Dusk – Intuitive and Easy Browser Testing for All!
Feb 23, 2017 @ 12:54:06

On the SitePoint PHP blog there's a tutorial posted that introduces you to Laravel Dusk, a browser-based testing tool, and how it can be used to test a Laravel-based application.

End to end testing for JavaScript applications, particularly single-page-apps, has always been a challenge. To that end, Laravel released its 5.4 version recently with a new testing library: Dusk.

With the release of Dusk, Laravel hopes to give its users a common API for browser testing. It ships with the default ChromeDriver, and if we need support for other browsers, we can use Selenium. It will still have this common testing API to cater to our needs.

The tutorial then walks you through the installation process and two approaches to getting it integrated into your application. They then create a first test, checking to see if a user can log in successfully. They also include how it looks when a test fails and the screenshot that's taken just before the failure. It also covers the testing of Ajax-related calls, inserting a delay when a button is clicked to wait for the response. Finally, the tutorial shows a more advanced example involving a popup modal, a form and multiple interactions.

tagged: laravel dusk browser testing tutorial introduction ajax example

Link: https://www.sitepoint.com/laravel-dusk-intuitive-and-easy-browser-testing-for-all/