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

Marc Baker:
Discharging Static #1
Mar 14, 2018 @ 14:49:47

On his blog Marc Baker about static calls and the trouble they bring in your applications. It's a continuation of ideas that Kore Nordmann shared previously on his site.

It’s been seven years since Kore Nordmann first published “static considered harmful” on his blog, explaining the problems with using static method calls in classes, and the difficulties that they cause when trying to test that class. Seven years on, and those difficulties are still the same, and there is still new code being written using static calls despite that knowledge; but it’s often a more severe problem in legacy code with little or no unit tests.a

So why exactly are static calls so bad? If you’ve read Kore’s article, then you probably have a good idea already; but what that article doesn’t cover is approaches that we can use to make the code testable.

He covers the main issue static methods have when it comes to testing: they introduce coupling by hard-coding a dependency into your code. He talks about the static testing functionality that various PHP testing tools provided: PHPUnit, Phake and Mockery. He then focuses on a newer tool that he's discovered to help make the testing simpler: AspectMock. He gives an example of it in use and some examples of tests using anonymous classes to make it easier to create tests on the fly.

tagged: static testing phpunit example harmful tutorial aspectmock

Link: https://markbakeruk.net/2018/03/13/discharging-static-1/

Laravel News:
User Defined Schedules in Laravel
Mar 09, 2018 @ 11:43:31

On the Laravel News site there's a new post looking at the use of user-defined schedules and some of the "hidden" features the Laravel framework provides to allow for even more customization.

Recently Adam Wathan and Taylor Otwell have used Basecamp to track what they are doing every day–a tech diary. Notably, they are using the check-in feature which allows you to schedule questions to be sent to members about almost anything. Taylor and Adam are using it for “What did you work on today?”

It’s not that it’s the same time of day or even that it’s only weekdays which peaked my curiosity but the fact every check-in has a custom schedule defined by a user. In Laravel, we can quickly schedule a job to run every weekday at 4 pm, and we can use a custom timezone. But out of the box, we cannot set a different schedule for every individual check-in. We would have to store a cron expression and manually check every minute if it’s due.

After some "source code diving" the author figured out that the Schedulable trait allows you to define a more customized version of a schedule to fit your needs, making it simple to implement in any class or model. Code examples of these changes are included in the post as well as some next steps to have it implemented more widely either in the Laravel core or a package.

tagged: laravel schedule custom trait tutorial example

Link: https://laravel-news.com/user-defined-schedules-in-laravel

Frank de Jonge:
Being in control of time in PHP
Mar 08, 2018 @ 10:49:18

Frank de Jonge has a new post to his site covering something that always seems to be a difficult topic in development: time. In his post he suggests that date and time handling in your application is a "dependency" that could be difficult test.

When developers talk about the infrastructural boundaries or external dependencies they often talk about databases and third-party integration. They're the first thing we'll put behind an interface so we can stub them out during our tests. This gives us some control over them. It's become relatively easy to spot these dependencies because we do it frequently. They're the usual suspects.

However, some "dependencies" are much harder to spot. They even live right inside the standard library of PHP and often manage to seep through the cracks. Date/Time handling is such a thing. So what's the problem and how do we fix it?

He goes on to talk about date/time handling programming languages in general and how its variance can can cause issues that might be out of our control. He suggests that when time "gets the best of you" you should opt to be more specific in your date/time handling (the cause is usually precision). He then gets into some code examples of how to "control time" by reducing the impact that direct date/time handling could have on your application. He also includes an example of testing this handling and finishes with the idea of "consuming time as a service".

tagged: datetime control test unittest example dependency

Link: https://blog.frankdejonge.nl/being-in-control-of-time-in-php/

Design Patterns in PHP
Mar 06, 2018 @ 11:18:33

On the Script-Tutorials.com site they've posted a lengthy tutorial that covers many common design patterns - 23 of them - and how they could be implemented in PHP.

Today we are going to talk about design patterns in web development, more precisely – in PHP. Experienced developers are probably familiar with this, but this article will be extremely useful for all novice developers. So, what is it – design patterns? Design Patterns aren’t analysis patterns, they are not descriptions of common structures like linked lists, nor are they particular application or framework designs. In fact, design patterns are “descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.” In other words, Design patterns provide a generic reusable solution to the programming problems that we encounter every day.

[...] Design patterns not only make software development faster, but also encapsulate big ideas in a simpler way. Also, be careful not to use them in wrong places in order to avoid unpleasant situations. In addition to the theory, we also give you the most abstract and simple examples of design patterns.

The tutorial starts with a table listing out the category (purpose) of the pattern, the design pattern name and some of the aspects of it that could vary depending on interpretation. The article then goes through each of the 23 patterns and includes the code to implement them. There's not much in the way of description about each but there are one or two sentences about its primary use.

tagged: designpattern implementation example code tutorial

Link: https://www.script-tutorials.com/design-patterns-in-php/

The DRY principle misunderstood
Feb 23, 2018 @ 11:55:36

On the Web-Techno.net site there's a new tutorial posted that talks about the DRY principle in development (Don't Repeat Yourself) and why you might be misunderstanding the intent.

I know what you are thinking: "Again a boring article on DRY? Don't we have enough already?". You might be right. However I see too many developers (junior or senior) applying DRY like they are doing some witch hunting. Totally randomly or everywhere they can. So apparently we never have enough DRY principle articles on Internet.

A little reminder for those in the back who don't follow: the DRY principle means "Don't Repeat Yourself" and was first introduced in the The Pragmatic Programmer. The principle itself was known and applied before this book came to life. However the Pragmatic Programmer defined it precisely and put a name on it.

He starts by defining the original intent of the principle: not repeating knowledge (not necessarily repeating code). He gives an example of a e-commerce site and the logic around "shipments". If there's logic around these shipments happening in multiple places in the application, that can lead to maintenance issues down the line. He suggests creating a single instance of the knowledge about shipment handling rather than just reducing code duplication. He gives an example of a product instance and code duplication happening in some of the data validation. After showing a possible solution for this particular issue he gets into some issues that come from over-DRYing your code including useless abstractions and premature optimization.

tagged: dry dontrepeatyourself principle knowledge example explaination

Link: http://web-techno.net/dry-principle-explained/

Laravel News:
5 Laravel Helpers to Make Your Life Easier
Feb 22, 2018 @ 09:45:32

On the Laravel News site they've posted a new article with a listing of five useful helpers that come standard as a part of the Laravel framework.

There are a ton of helper methods in Laravel that make development more efficient. If you work with the framework, I encourage you to see what helpers you can introduce in your day-to-day work. In this blog post, I’d like to point out a few of my favorites.

The list in the post includes helpers for locating information in an array, pluralizing strings, throwing exceptions based on conditions and accessing object property values. For each item on the list there's a brief explanation and some sample code showing it in action (and what the result ends up being). The post finishes up by linking you over to the helpers page of the Laravel manual for more information and other handy functions to help reduce your own development time.

tagged: laravel helper simple top5 list example code

Link: https://laravel-news.com/5-laravel-helpers-make-life-easier

Christoph Rumpel:
The BotMan WebDriver explained
Feb 14, 2018 @ 10:23:10

In this post to his site Christoph Rumpel covers a feature of the Botman chatbot library in a bit more depth: the WebDriver making interaction with the bot in a current application simpler.

The WebDriver is one of the best features about the BotMan library. Still I see a lot of people struggling with the concept and how to use it. Let's clear things up and let me show you how I already used this driver in production.

He starts off by talking about chatbots in general and how they're "more than just messengers" and can provide more real-time information to visitors to your site. In order to enhance the basic bot functionality, it needs to work with another source - in this case an API accessed via the WebDriver. He then gets into some example code showing how to use the driver to access an API and some explanation of the pieces involved.

tagged: botman chatbot webdriver driver tutorial introduction example

Link: https://christoph-rumpel.com/2018/02/the-botman-webdriver-explained

Keith Mifsud:
PHP Software Development Workflow
Jan 22, 2018 @ 12:30:02

In a post to his site Keith Mifsud shares his recommendations around a good workflow for developing PHP applications. Composer and GitHub play a part in the process as does the use of Git and your IDE of choice.

Continuing from the previous post Developing a Command Bus in PHP, we will look into how to get started in setting up our composer library project. Although my PHP software development workflow does vary slightly from project to project, most steps are more or less the same. Whether your project is the result of a successful proposal or you want to build an open source composer package like we’re doing here, this post will guide you in working with PHP the right way. If you are learning PHP from scratch and find that I am missing some intermediary steps or making assumptions, please feel welcome to comment below and I’ll be more than happy to explain in more detail.

He then walks through the steps in the process, each with explanation (and screenshots where appropriate):

  • Getting organized and planning the application features
  • Creating the GitHub repository and setting up the Kanban board
  • Defining a release milestone
  • Setting up a local Homestead instance for development
  • Setting up Git and your IDE

The final step is to create the composer.json configuration file that defines the basic information about the project, any dependencies and the autoloading paths.

tagged: software development workflow example process composer

Link: https://keith-mifsud.me/php-software-development-workflow

Sergey Zhuk:
Managing ReactPHP Promises
Jan 18, 2018 @ 10:50:01

In a new post to his site Sergey Zhuk has a tutorial showing you how to manage promises in ReactPHP. Since promises are fired asynchronously they can be difficult to manage and use their output across the application.

Asynchronous application is always a composition of independently executing things. In concurrency, we are dealing with a lot of different things at once. [...] So, to make concurrency work you have to create a communication between these independent parts to coordinate them. And here come promises. They are the basic unit of concurrency in an asynchronous application. They are the blood of the asynchronous application and move the results between different tasks across the code.

He then covers a few different situations and offers advice on how to more correctly handle them:

  • I don’t know exactly what the resolver will give me
  • I want to reject a promise but without throwing an exception
  • I want to run multiple tasks and when they all finish do something else
  • I have some pending tasks and want to continue once I receive the first feedback
  • I have some pending tasks and want to continue once the first one is completed
  • I have some pending tasks and want to continue once a certain number of tasks will be completed

Code is provided for each of the situations giving you an easy, ready to use example for your application. Most require only a few lines to get the job done and can be very useful in the right circumstances.

tagged: reactphp manage promises situation code example tutorial

Link: http://sergeyzhuk.me/2018/01/16/reactphp-managing-promises/

Brandon Savage:
Don’t write useless unit tests
Jan 17, 2018 @ 10:44:42

Brandon Savage has a quick post to his site sharing some advice around the testing of your application, more specifically around unit tests: don't write useless unit tests. He starts with an example of a test that, while moving the project closer to the 100% coverage number, is mostly useless.

Too often, in the search for 100% unit test code coverage, I see tests like this get written. They don’t serve a practical purpose, except to meet the test coverage goal. Worse, they don’t actually improve the quality of the application.

Instead of writing a unit test here, we would be better served by writing an integration test, or a functional test. These tests would require us to interact directly with the database, but would provide far more valuable information about the health and status of our application. A useless unit test provides us with little if any benefit; a useful functional test provides us with tremendous advantages.

He includes the code for the test and talks about what's wrong with the approach and how it could potentially be handled better. He suggests that writing good, useful tests requires both skill and determination and the avoidance of tests that actually increase the quality of the overall test suite.

tagged: useless unittest tutorial example functional test

Link: https://www.brandonsavage.net/dont-write-useless-unit-tests/