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

Sameer Borate:
Using the TOML configuration format in your applications
Aug 01, 2018 @ 17:54:59

In this new post to his site Sameer Borate looks at using the TOML configuration file format in a PHP application.

As any one who has programmed knows about configuration files. Configuration files are mostly text files used to configure the parameters and initial settings for computer programs – mostly user applications, operating system settings.

[...] In this post we will look into parsing TOML files in PHP. TOML is a configuration file format that is intended to be easy to read due to its obvious semantics and is designed to map unambiguously to a dictionary data structure. “TOML”, the name, is an acronym for “Tom’s Obvious, Minimal Language” referring to its creator Tom Preston-Werner.

He starts with an example of a configuration file in the TOML format and, following the installation/used of the yosymfony/toml library, the result of it being parsed. He also includes an example of using the same library to build out a TOML configuration and dump it to a string for output.

tagged: toml configuration file format introduction tutorial package

Link: https://www.codediesel.com/data/using-the-toml-configuration-format-in-your-applications/

Laravel News:
Introduction to TOML Configuration in PHP
Jul 30, 2018 @ 14:37:31

On the Laravel News site today there's a tutorial posted introducing you to TOML configuration, a new configuration file structure that's designed to be easy for humans to read and highly flexible (all while staying relatively simple).

TOML is a configuration file format language that is intended to be minimal and easy to read. TOML stands for “Tom’s Obvious, Minimal Language,” which refers to the creator Tom Preston-Werner.

[...] TOML aims to be a minimal configuration file format that’s easy to read due to obvious semantics. TOML is designed to map unambiguously to a hash table. TOML should be easy to parse into data structures in a wide variety of languages.

In order to work with it in PHP (since there is no native support) they show you how to install the yosymfony/toml package. The tutorial then walks you through an example TOML configuration file and what the result of using the package to parse it looks like. It then takes it a step further an converts one of the database configuration files for a Laravel application to the TOML format. It also shows the reverse - using the package to create a TOML file and the resulting output.

tagged: toml configuration file custom format introduction tutorial

Link: https://laravel-news.com/toml-configuration-in-php

Adam Wathan:
Cleaning Up Form Input with Transpose
Apr 15, 2016 @ 16:50:34

Adam Wathan has a post on his site showing you how to use "transpose" functionality to clean up form input, transforming a set of arrays from submitted data back into a better structure.

Transpose is an often overlooked list operation that I first noticed in Ruby. The goal of transpose is to rotate a multidimensional array, turning the rows into columns and the columns into rows.

In his example, he adding multiple "contacts" at once. He shows the difficulties with this, the form structure and the data returned. There's several formats that could be returned but none are the correct structure to push into a model (his examples are in Laravel there's a generic array_map example too) He shows how to update the Laravel collection handling using a custom macro, transpose, to handle the array_map functionality in a more self-contained way. He includes the code to make the macro work and an example of it in use to correctly format his incoming contact data into something he can use in his collection.

tagged: transpose laravel collection array data format arraymap

Link: http://adamwathan.me/2016/04/06/cleaning-up-form-input-with-transpose/

Ross Tuck:
Formatting Exception Messages
Oct 27, 2015 @ 17:09:39

In a post to his site Ross Tuck shares some of his experience and some helpful hints around formatting exception messages and how doing so effectively can make life for fellow developers much easier.

Over the last couple years, I’ve started putting my Exception messages inside static methods on custom exception classes. This is hardly a new trick, Doctrine’s been doing it for the better part of a decade. Still, many folks are surprised by it, so this article explains the how and why.

He shares his tips as a part of a "refactoring" in a simple example, a CSV import where there are failures during the import process on certain lines. He starts with the basic Exception and works through the logic to customize it and make it more useful. He shows the inclusion of additional details in the message, abstracting out the formatting to custom methods based on the error type and using static methods for the more complex message formatting. He also suggests the creation of methods to handle specific error cases with more details than a simple single-line error in a normal exception being thrown.

When you co-locate the messages inside the exception, however, you gain an overview of the error cases. If these cases multiply too fast or diverge significantly, it’s a strong smell to split the exception class and create a better API. [...] Sometimes we underestimate the little things that shape our code. [...] Creating good environments at a high level starts with encouraging them at the lowest levels. Pay attention to what your habits encourage you to do.
tagged: format exception message custom method details static

Link: http://rosstuck.com/formatting-exception-messages/

ServerGrove Blog:
Introduction to the PHAR format
Jul 31, 2015 @ 17:15:44

The ServerGrove blog has posted an introduction to the PHAR format, a built-in method to use PHP and create self-contained functional scripts as a single *.phar file making it much easier to transport.

In the last years there is a trend in the PHP community to release tools, especially command line utilities, as PHAR files, so you can package an entire PHP application into a single file for convenience. But, how PHAR files work? In this post we will try to explain it.

They cover a few of the basic topics first: what is a PHAR file and a few examples of them being provided by major PHP projects. They then get into the creation of an archive, showing how to make a super simple PHAR "Hello World" archive, created with just a bit of PHP. They then get into the structure behind the archive and get into detail on each section (stub, manifest, file contents and signature).

tagged: phar format introduction archive package

Link: http://blog.servergrove.com/2015/07/30/introduction-phar-format/

Evert Pot:
jCard is now a thing
Jan 21, 2014 @ 17:04:18

In his most recent post Evert Pot talks about jCard, a JSON-based format that was recently approved to serve up VCard personal information data in an easier-to-parse format.

I'm a big fan of this format. vCards have been around since 1995, and even though we've had a pretty significant update in 2011 in the form of vCard 4.0, the format is still complicated to parse, has a number of problems that go all the way back to the early days. [...] The biggest problem with vCards, is that upon a first glance, the format seems extremely easy to parse and generate with just a couple of string manipulation functions. When you dig deeper into the specifications though, you'll notice that it's actually really complex and hosts a ton of edge cases.

He includes an example of how to generate the jCard format using the Sabre/Object and the resulting output, both in the traditional vCard format and the new jCard structure.

tagged: vcard jcard json format sabre object parse output

Link: http://evertpot.com/jcard-completed

[php]architect:
Returning to Print!
Oct 16, 2013 @ 14:29:06

[php]architect magazine has made a big announcement today in response to one major request they've received - the magazine is once again available in a print version!

"Are you going to bring the magazine back to print?" It's the #1 question on everyone's minds. Since musketeers.me first acquired php[architect] at the beginning of 2013, it is the one question that we have constantly been asked. While digital publishing is the future, people still love the feeling of having a printed magazine in hand.

Knowing that our customers wanted it, we've worked hard to figure out a way that we could bring the magazine back to print again. We are extremely excited today to announce that starting with the November 2013 issue, php[architect] will once again be printed!

The print subscription will come in at $119 USD for a year's worth of magazines. They've also updated all current customers to a combined Digital level that includes all three major formats (PDF, ePub and Mobi). You can get more information about your current subscription level by logging in or, if you want to sign up now, check out the subscription information.

tagged: print version phparchitect magazine subscribe digital format

Link: http://www.phparch.com/2013/10/returning-to-print/

Derick Rethans:
ISO 8601 week dates
Sep 24, 2013 @ 14:54:31

Derick Rethans has a new post with details about handling ISO 8601 dates in PHP via the DateTime functionality. It's a response to some bugs filed having to do with week numbering.

Week numbers are defined in this same ISO 8601 standard. Each year has 52 or 53 weeks and weeks always start on a Monday. Week number 1 of each year is the first week in a year that has the first Thursday of the year, or in other words, the week containing January 4th.

He talks about some of the date format arguments that use would use when working with the ISO 8601 formatting and how it relates to the calendar year. He points out that the "Y" format specifier is not the same as the "o" - the first being the calendar year while the second relates to the ISO 8601 year.

As conclusion, this article shows that there are two ways representing dates in PHP. In the Gregorian1 calendar with year, month and day (of month), and in the ISO 8601 calendar with year, week and day (of week). The format characters for the two different years are either Y or o and they should not be confused.
tagged: iso8601 date datetime format specifier gregorian calendar difference

Link: http://derickrethans.nl/iso-8601-weeks.html

Phil Sturgeon:
Why PHP DateTime Rocks
Aug 02, 2012 @ 16:41:29

Phil Sturgeon has a new post sharing some of his thoughts on why DateTime rocks, the advanced functionality that PHP has to work with dates, times, timezones, etc.

DateTime is nothing new, but it's definitely under-used by many. It was made available in PHP 5.2.0 but got some of its best features until PHP 5.3.0. PHP 5.3.0 is pretty old now, but I learned about DateFormat::createFromFormat() after reading a new addition to PHP The Right Way: Date and Time.

He shares to "offender" examples where using this function allowed him to simplify and reduce the amount of code needed to handle the formatting of a date into a MySQL format and calculating the difference between two time values.

tagged: datetime example mysql data format createfromformat

Link:

Bob Majdak:
Apache access logging in JSON format
Jul 26, 2012 @ 16:42:02

Bob Majdak has a quick new post to his site showing you how to log messages to Apache using a JSON format instead of the usual single-line information.

So this past week I have been doing a lot of logging, parsing, and graphing. I was sitting there wondering what I could do to make the Apache access logs easier to work with. Then it hit me, a brilliant stroke of win: why not format it in JSON? Then in my log parser I could just json_decode it! As far as I know there are no tricks to make Apache do this for you, so really all we have to do is define a custom log format that happens to look like JSON.

He includes the "LogFormat" line you'll need to add to your Apache configuration to make it log the JSON and a bit of PHP to help you pull in the file's contents and parse each line.

tagged: json apache log format logformat jsondecode

Link:


Trending Topics: