PHP has a use statement that allows the importing or, more accurately, aliasing of classes into the current namespace. In other languages, like Python, importing a nonexistent class or module will fail at the import. In PHP, this doesn't happen, instead the program will fail later when the aliased class, function, or constant is is… Continue reading In PHP Use is More Like an Alias than an Import
Category: PHP
Lifecycle Objects as Extension Points
Something I've found myself doing more and more is writing lifecycle objects that add notification-like extension points to other objects. A good example is the MessageLifecycle interface and its implementations in PMG's queue library. Rather than pollute the queue consumers with events or other more generic things directly, the lifecycle provides and extension point into… Continue reading Lifecycle Objects as Extension Points
Multi-Stage Docker Builds for PHP Applications
The neatest thing I've discovered about Docker in the last few months are multi-stage builds. To put it simply a multi-stage build is a Dockerfile with two or more FROM stanzas. Let's explore what that means for a PHP project. First, a goal: the images built for any application should 100% ready to go. That… Continue reading Multi-Stage Docker Builds for PHP Applications
Parameter & Result Objects: More Than Grouping Values
A parameter object replaces one or more parameters to a method with a single object instance. A result object is a object created specifically for a return value from a method. Parameter Objects Changing a method to accept a parameter object is a common refactoring for grouping parameters that belong together. Unfortunately that's not the… Continue reading Parameter & Result Objects: More Than Grouping Values
PHPSecLib SFTP Error Handling
In the marketing world, the de facto way to send data around is a file. Usually via SFTP -- five years ago it was almost 100% FTP. Occasionally a third party tool will have an API, but clients will almost always only deliver things like product feeds or first party revenue data via a file.… Continue reading PHPSecLib SFTP Error Handling
Logging to Papertrail from Symfony Applications
PMG uses Papertrail to aggregate logs and we've found it to be really solid over the last four years. Example Monolog Configuration Here's a quick example of configuration for Monolog Bundle to get logs to papertrail. This uses Monolog's remote syslog handler to do its work. Couple key points around the services here: PsrLogMessageProcessor is… Continue reading Logging to Papertrail from Symfony Applications
On Library Exceptions
https://twitter.com/chrisguitarguy/status/915313560036741120 I made this statement on twitter about PHP libraries throwing exceptions that I want to expand here. I'd say there are two broad categories of exceptions that a library might throw. Domain Exceptions These are specific to the library's domain. For instance, if I'm working with AdWords I know that AdWords account IDs take… Continue reading On Library Exceptions
AWS Key Management Service Envelope Encryption in PHP
This is a PHP example of what AWS calls envelope encryption. Really this is just a way to use a key hierarchy rooted at a key management service (KMS) key. We'll use PHP 7.2's libsodium support (via paragonie/sodium_compat). The idea is that you have a customer master key that lives in KMS - this never… Continue reading AWS Key Management Service Envelope Encryption in PHP
Extracting GZIP Files with PHP
PHP comes with a nice zlib extension in its core. We're going to use it to extract a gzip compressed file. The steps here are pretty simple: Open the gzip file with gzopen Open the destination file with fopen Transfer the data from the gzip file to the normal file Close the open resources Most… Continue reading Extracting GZIP Files with PHP