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

SitePoint PHP Blog:
Event Sourcing in a Pinch
Nov 30, 2016 @ 10:56:26

Christopher Pitt is back with a new tutorial on the SitePoint PHP blog talking about event sourcing in PHP including a brief explanation about what it is and how it can be useful in your PHP application.

Let’s talk about Event Sourcing. Perhaps you’ve heard of it, but haven’t found the time to attend a conference talk or read one of the older, larger books which describe it. It’s one of those topics I wish I’d known about sooner, and today I’m going to describe it to you in a way that I understand it.

Christopher then gets into some of the basic concepts behind event sourcing, a part of Domain Driven Design, and the difference between storing state and storing behavior. With this outlined he gets into the creation of the actual event handlers with examples from a retail application (orders, outlets, stock, pricing, etc). He includes the code for several simple events, a method for recoding them in your database and some helper functions to translate the event to the SQL required for the insert operation. He then links these with the event classes and putting them to use, executing them and getting the results back via a sort of "layer" between the fetch and the response.

tagged: eventsourcing tutorial introduction example domaindrivendesign

Link: https://www.sitepoint.com/event-sourcing-in-a-pinch/

QaFoo Blog:
Using Traits With PHPUnit
Nov 29, 2016 @ 12:26:19

The QaFoo site has a new post showing an interesting practice that could be used in your PHPUnit tests to provide additional functionality without the need for complicated inheritance - the use of traits.

As we already wrote that "Code Reuse By Inheritance" has lots of problems and we consider it a code smell. You should always aim to use Dependency Injection, most likely Constructor Injection. But with test cases in PHPUnit we cannot do this because we have no control about how and when our test cases are created. There are a similar problem in other frameworks, like we discussed in "Object Lifecycle Control". We also blogged about traits as a Code Smell, but let me show and explain why they might be fine to use in your test cases.

They provide an example of where the use of traits might be acceptable starting with a simple test case to check the login behavior with an invalid password. This uses an "is a" inheritance relationship with a parent test class with setUp/tearDown method. This refactored a bit to make use of traits to provide common login functionality based on methods in a trait. The post wraps up talking about traits as a "code smell" despite them seemingly making the test code cleaner, mostly that it limits the ability to change functionality by simply changing the associated code.

tagged: traits phpunit tests code smell example tutorial

Link: https://qafoo.com/blog/092_using_traits_with_phpunit.html

SitePoint PHP Blog:
How to Properly Deploy Web Apps via SFTP with Git
Nov 29, 2016 @ 11:53:49

On the SitePoint PHP blog there's a new tutorial posted showing you how to properly deploy applications with SFTP and Git. In their examples they build a PHP-based deployment process that uses a few handy packages to make the flow simpler than a set of manual commands.

Uploading files is an integral aspect of any deployment process, and the underlying implementation can vary depending on the type of your server.

[...] The PHPSECLIB (PHP Secure Communications Library) package has an awesome API for routine SFTP tasks: it uses some optional PHP extensions if they’re available, and falls back on an internal PHP implementation otherwise. You don’t need any additional PHP extension to use this package, the default extensions that are packaged with PHP will do. In this article, we will first cover various features of PHPSECLIB – SFTP, including but not limited to uploading or deleting files. Then, we will take a look at how we can use Git in combination with this library to automate our SFTP deployment process.

They start with a quick command (Composer) to get the phpseclib library installed but then quickly move into using it and some SSH keys to:

  • authenticate to the server with public/private keys
  • uploading a sample file
  • automating the deployment with Git, pushing only changed files from a local git repo
  • getting the contents of a specific commit
  • the actual push of the files via SFTP

There's also a few other helpful hints included showing how to manage permissions on the remote server, execute remote commands and downloading files. The post ends with links to other similar tools if you're interested in more complete approaches.

tagged: deploy application sftp git deployment tutorial phpseclib example

Link: https://www.sitepoint.com/how-to-properly-deploy-web-apps-via-sftp-with-git/

MyBuilder Tech Blog:
Using Constraint-based Ordering in PHP
Nov 28, 2016 @ 11:09:18

On the MyBuilder.com Tech blog they've posted a tutorial from Edd Mann looking at the use of constraint-based ordering in PHP applications.

An interesting problem arose last week when we wished to generate a listing of recently completed jobs (along with their shortlist fees). Upon review of some earlier attempts, we did not like the aesthetics present when many of a particular shortlist fee were clustered together (i.e. two or more adjacent jobs with the same shortlist fee). What we were instead looking for was to create a constraint-based ordering that when applied to the recently completed jobs, would give an even distribution of shortlist fees (data-set permitting).

Initially he tried a "low-high" ordering method based on their "fee" value. Instead, to make the solution more "random" he reframed the need and broke it down into three separate actions: sort, partition and interleave. As PHP only has one of those, he came up with his own solutions for the other two (code included). He explains briefly how it all works together and what kind of benefits it has over the low-high solution originally created.

tagged: tutorial constraint based ordering example partition interleave

Link: http://tech.mybuilder.com/using-constraint-based-ordering-in-php/

Leonid Mamchenkov:
Dependency resolution with graphs in PHP
Nov 22, 2016 @ 10:52:23

Leonid Mamchenkov has a post to his site showing how he solved an interesting problem in one of his recent projects: determining the order to use items based on their dependencies.

One of the projects I am working on at work presented an interesting problem. I had a list of items with dependencies on one another and I needed to figure out the order in which to use those items, based on their dependencies.

He gives the example of database tables where it would be required to export the tables so that the relations between them are maintained when imported back in. He gives some example data, a basic nested PHP array, and defines the relationships between them (just strings in this case). While he did solve the problem, he wasn't happy with the solution. Instead he went looking for other options and found graph theory to be a good match. He briefly cover what the theory involves and links to an example that basically does what he needs...but is written in Python. He finishes off the post sharing his refactoring of this logic into PHP including a recursive "dependency resolver" and the output showing the correct ordering for loading objects based on their dependencies.

tagged: resolve dependency graph theory example tutorial load order

Link: http://mamchenkov.net/wordpress/2016/11/22/dependency-resolution-with-graphs-in-php/

Barry van Veen:
Laravel service provider examples
Nov 15, 2016 @ 09:44:31

In this recent post to his site Barry van Veen introduces you to providers in Laravel applications and how they relate back to the built-in service provider.

Currently, I'm working on my first Laravel package. So, it was time to dive into the wonderful world of the service container and service providers.

Laravel has some great docs about, but I wanted to see some real-world examples for myself. And what better way than to have a look at the packages that you already depend on?

This post details the different things that a service provider can be used for, each taken from a real open-source project. I've linked to the source of each example.

He starts off talking about (and linking to) the current provider documentation and includes a basic example of adding the provider to your configuration. He then covers several different pieces of functionality that can be used inside the providers including:

  • binding of singletons and instances
  • setting up aliases
  • registering dependencies
  • adding in additional resources

There's also a few "other" things included showing how to defer loading, set up event listeners and adding in a new Blade directive.

tagged: laravel service provider introduction tutorial example

Link: https://barryvanveen.nl/blog/34-laravel-service-provider-examples

Joe Watkins:
Expanding Horizons
Nov 03, 2016 @ 09:40:49

In his most recent post Joe Watkins talks about a PHP extension he's been working on that wraps the libui library making it easier to use PHP to create cross-platform user interfaces.

Recently I have been working on a new extension. It is a wrapper around libui, which is a cross platform user interface development library, that allows the creation of native look and feel interfaces in the environments it supports.

That's a few hundred lines of PHP 7 code, moulded into an imitation of the snake game we all used to have on our phones. We've seen other user interface extensions before in PHP, there's even a modified PHP runtime that will allow you to write GTK+ applications.

I don't know anyone that ever deployed any of those extensions, and for very good reasons; PHP5 can barely do anything without allocating a bunch of memory, and doing a bunch of other extremely inefficient things, almost everything it does is inefficient. Beyond a basic forms like application, PHP5 is close to useless.

You can see an example of the snake game in action in this YouTube video. He goes on to talk about the low amounts of CPU and RAM the game (and extension) use and that, with the right amount of work, it can achieve around 60 frames per second. He points out that it is still early on in the development cycle for the extension and libui but there's already documentation for those wanting to investigate.

tagged: libui extension example video snake game efficiency

Link: http://blog.krakjoe.ninja/2016/11/expanding-horizons.html

Scotch.io:
Understanding PHP Generators
Oct 28, 2016 @ 11:20:44

The Scotch.io blog has posted a tutorial that wants to introduce you to and help you understand a feature recently added to the PHP language: generators. In this new article author Samuel Oloruntoba walks you through some of the basics and offers advice on when to use this helpful feature.

When it comes to driving, speed is not everything. But on the web, speed makes all the difference. The faster your application, the better the user experience. Well, this article is on PHP Generators, so why are we talking about speed? As you are soon about to find out, generators make a huge difference on speed and memory management.

He starts off by explaining what generators are and gives a simple code example showing how they can replace a standard loop (without some of the memory overhead). He uses a for and foreach loop to show a memory overflow caused by it trying to reach the highest integer allowed in the config. To help solve this, he makes use of generators, a much more memory efficient method that only returns the latest value requested and doesn't keep the remainder in memory. He then answers the question of why you might need to even use generators, how to return keys and send values into the generator. He also offers a word of advice on using them - mostly to not overuse them as it's still possible to have issues (like the memory one above).

tagged: generator example introduction tutorial memory yield

Link: https://scotch.io/tutorials/understanding-php-generators

SitePoint PHP Blog:
A First Look at Atlas – the ORM That Delivers
Oct 17, 2016 @ 15:16:33

The SitePoint PHP blog has posted a new tutorial focusing on the Atlas ORM, a recent addition to the wide range of database ORMs in the PHP ecosystem, focusing on being a mapping of your persistence model.

By definition, a Data Mapper moves data between objects and a database and isolates them from one another. With a Data Mapper, the in memory objects don’t even need to know that a database exists. It does not need to know the SQL interface or database schema; it doesn’t even need the domain layer to know it exists!

This might lead us to thinking that, in Atlas, the persistence layer is totally disconnected from the database, but that is not quite what happens. [...] An Atlas Record is passive; not an active record. Unlike most ORMs, its objects represent the persistence model, not the domain model. Think of it as representing how the data is stored and not as real world representations.

The tutorial goes on to talk about some of the background behind the package being developed and some of its core principles. They then walk you through the installation of the package, doing a bit of related database setup and the code to perform some basic CRUD (create, read, update, delete) operations on the tables. This is followed by a few more practical examples and a few caveats for the library's use.

tagged: atlas orm database tutorial example crud operation

Link: https://www.sitepoint.com/a-first-look-at-atlas-the-orm-that-delivers/

Toptal.com:
Buggy PHP Code: The 10 Most Common Mistakes PHP Developers Make
Oct 07, 2016 @ 11:12:27

On the Toptal.com site a new article has been published from author Ilya Sanosyan with his list of top ten development mistakes he sees PHP developers make leading to more buggy code.

PHP makes it relatively easy to build a web-based system, which is much of the reason for its popularity. But its ease of use notwithstanding, PHP has evolved into quite a sophisticated language with many frameworks, nuances, and subtleties that can bite developers, leading to hours of hair-pulling debugging. This article highlights ten of the more common mistakes that PHP developers need to beware of.

Included in his list are common issues like:

  • Leaving dangling array references after foreach loops
  • Confusion about returning by reference vs. by value
  • Performing queries in a loop
  • Assuming $_POST will always contain your POST data
  • Ignoring coding standards

For each item on his list he provides good code examples and explanation of both why it's an issue and what can be done to prevent it.

tagged: top10 common mistakes programmers developers list example code

Link: https://www.toptal.com/php/10-most-common-mistakes-php-programmers-make