Overwriting Command Line Output With PHP

12th April 2020 - 5 minutes read time

The other day I was trying to print some output to the command line and then overwrite the output afterwards. It turns out that there are a couple of ways to do this so I thought I would detail a few of them here.

By far the simplest way of doing this is to run the "clear" command, which we can run via the system() function in PHP. This will clear the output of the command line ready for you to print out whatever you need. The downside to this is that the entire terminal window is cleared. Another downside is that once the output is complete scrolling up will reveal the output that was cleared out.

Conway's Game Of Life In PHP

12th April 2020 - 9 minutes read time

I was saddened to hear of the passing of the Mathematician John Horton Conway today so I decided to put together a post on his 'game of life' simulator.

This game of life, more commonly called Conway's game of life, was devised by John Conway in 1970 and is a way of modelling very simple cell population dynamics. The game takes place on a two dimensional board containing a grid of orthogonal cells. The game is technically a zero player game in that the initial setup of the game dictates the eventual evolution of the board.

The rules of the game (taken from wikipedia) are as follows.

Tidying Up A Git Repo

4th April 2020 - 5 minutes read time

Creating branches in git basically free and is easy to do. This has lead to the development of techniques like git flow and other branch related work streams, which are now commonplace in development.

If you have a project being worked on by many developers you can get into the situation where there are many branches in the project. Some will be merged, some will be worked on currently and some more will be branches that were worked on, but never finished (for one reason or another). This can lead to some confusion over what branches are being worked on, especially if you want an oversight on the current health of the project.

Drupal 8: Creating A Views Results Area Plugin

23rd March 2020 - 3 minutes read time

Drupal Views is a great way of exposing data to users via a search interface. This can be done directly via the database or via a Solr server. Plenty of Views plugins exist to allow reacting to the search input and output in a variety of different ways.

The other day I needed to add a personalised message to Views output to inform a user that their search keyword didn't find any results. There is a plugin for Views that allows this, but it only shows a basic search string.

What I needed was a way to print out the following block of HTML, containing the search term that the user searched for.

DrupalCamp London 2020

18th March 2020 - 9 minutes read time

DrupalCamp London was held on the 14th and 15th March 2020 and I went down to City University London to partake in the event. I wasn't talking this year, so I was there purely to enjoy the conference.

Drupal 8: Theming With Tailwind CSS

8th March 2020 - 13 minutes read time

Upon a recommendation from someone in my local Drupal user group I decided to give Tailwind CSS a go. The ultimate aim of this was to replace the base theme I am using here with a more stripped down theme. At the time of writing this I am using the Cog theme, and whilst it has it's merits, I find that it's a little too much for this simple site.

I decided, therefore, to create a new theme and use Tailwind CSS to alter the site a little. This meant an exercise in integrating Tailwind CSS into a Drupal theme.

Tailwind CSS?

In case you haven't heard of Tailwind CSS I'll quote the site here.

"Tailwind CSS is a highly customizable, low-level CSS framework that gives you all of the building blocks you need to build bespoke designs without any annoying opinionated styles you have to fight to override."

Adventures In FizzBuzz

29th February 2020 - 8 minutes read time

Tests for programmers in an interview process are not uncommon. For the last couple of years I have asked a quick pre-interview question to junior developers to see what sort of stuff they come up with.

As I don't want to set any developer a task that will take longer than absolutely needed I opted to set a very simple task for them. Commonly known as "FizzBuzz", this task is as follows.

"Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"."

The expected output for this would be something like this.

Inverting A Scale

23rd February 2020 - 3 minutes read time

One technique I find useful, especially when drawing images, is to invert a number on a scale. In PHP, coordinates in an image are drawn from the top left of the image. This means that if we want to draw from the bottom left then we need to invert the y coordinate.

The following is example PHP code that creates an image with a single, diagonal line, drawn across the middle.

Drupal8: Finding All FieldWidget Types

16th February 2020 - 3 minutes read time

Whilst searching for an answer to a problem the other day I found a comment that details a command that prints out all of the FieldWidgets available in Drupal 8. This is useful if you are looking for all of the field widget classes in Drupal8.

Here is the command:

grep -rl '\@FieldWidget' | xargs grep -m1 'id = ' | grep -o FieldWidget.*

This prints out something similar to the following:

Traits Versus Inheritance In PHP

9th February 2020 - 6 minutes read time

The other day I was conducting a code review and found that a developer had used a trait to give two classes the same group of utility methods. Whilst there was nothing wrong with this in terms of functionality, I asked the developer why they had chosen to use traits instead of inheritance. We eventually decided that an inheritance model would be better suited to the situation but I thought I would go through some of the thought processes here.

What Is A Trait?

A trait, if you weren't aware, is like a class, but you don't instantiate it directly. Traits are defined using the trait keyword and are otherwise quite like a class in structure.

The idea is that code is essentially copied into the class you want to use it in from the trait and the class acts like it had that code all along. For example, let's take a simple trait.