Archive: page 3
Search and replace with confirmation in Bash
Automated search and replace can be very handy although there are occasions where a human needs to get involved on some of the decisions. If the search term isn’t unique or appears as part of other words or something like that. When this is the case you’ll want a confirm step where you can approve each replacement before it happens. With very little work we can achieve this using a combination of vim and grep. Read more ⇒
PHP and immutability: objects and generalisation - part three
In the last article we learnt how to create modified copies of an immutable in PHP. This one is going to tackle an issue I have hitherto skirted around and avoided. Objects in immutable data structures. This article is part of a series I have written on the topic of immutability in PHP code: Part one - a discussion of caveats and a simple scalar handling immutable Part two - improve the process of creating modified copies of the immutable Part three - objects in immutable data structures and a generalised immutable implementation Also available in Русский (Russian): Read more ⇒
PHP and immutability: modified copies - part two
In the last article we learnt how to create an immutable data structure in PHP. There were a few issues to work through, but we got there in the end. Now onto making the immutable class more useful and easier to create modified copies. Note that these are copies and not modifications, in-place, to the original objects. This article is part of a series I have written on the topic of immutability in PHP code: Read more ⇒
PHP and immutability: difficulties and scalars - part one
Being a weakly typed dynamic language, PHP has not really had the concept of immutability built into it. We’ve seen the venerable define() and CONSTANTS of course, but they’re limited. Whilst PHP does ship with an immutable class as part of it’s standard library, DateTimeImmutable, there is no immediately obvious method to create custom immutable objects. This article is part of a series I have written on the topic of immutability in PHP code: Read more ⇒
Alter a MySQL column in all databases
When you have a series of applications all running the same database structure it can be annoying to roll out schema updates across all the databases. If you’ve got migrations then great - script their deployment, but when you’re dealing with an old legacy application you probably don’t have the luxury. I was firmly in the latter class of devops when working on a project a couple of years ago so I wrote a handy little snippet of SQL to help me out. Read more ⇒
Email me when the file changes
It is important to ensure that Google does not index sites whilst they are still on a staging environment, but you cannot lock it down completely - how would your clients proof it? So I run a simple global rewrite rule in Apache that redirects all requests for robots.txt to a central disallow all response. This works great and Google appears to honour the rule as one would hope. What happens though when something about that central file changes? Read more ⇒
SQL style guide misconceptions
Many people have read, reviewed and even implemented the SQL style guide that I wrote. This is great, but there have also been a number of commonly held misconceptions or incorrect readings of the points made in the guide. I have decided to address some of the more common ones via a blog post that will, hopefully, clarify the situation. Basics A lot of people seem to have a very weird understanding of the basic terms used in the guide so here is what I mean when I say ‘Avoid’ and ‘Try’: Read more ⇒
Quick way to create a PHP stdClass
A very short and simple trick for creating new stdClass objects without having to set every property individually. This is akin to JavaScript’s object notation, but not quite as elegant. Creating a new object in JavaScript looks like the following example. const x = { a: "test", b: "test2", c: "test3", }; With PHP it is possible to use type casting to convert a simple array into a stdClass object which gives you a similar looking syntax although there is a little more typing required. Read more ⇒
Functional Programming in PHP Second Edition Available Now
It is with great pleasure that I announce the second edition of the Functional Programming in PHP book that I have been working on. There is twice the content of the first edition of the book as well as updates for PHP 7 and Facebook’s HHVM (HipHop Virtual Machine). There are now more functional techniques and patterns included with pipelines, pattern matching and flat maps among them. I have added a section of the book dedicated to the handy syntax and functionality that HHVM can provide functional programmers with. Read more ⇒
Importing and aliasing PHP functions
As a follow on to my short post about namespaces and functions from a year ago I thought it would be worth covering importing a specific function and aliasing functions via namespace operators too. This has been possible since PHP 5.6, but there is a nice addition in PHP 7 I’ll cover towards the end. In the previous article I demonstrated how you can namespace functions and use them, but as a refresher; you can enclose functions within a namespace just like a class. Read more ⇒