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

Adam Wathan:
Cleaning Up Form Input with Transpose
Apr 15, 2016 @ 11: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 @ 12: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 @ 12: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 @ 11: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

Returning to Print!
Oct 16, 2013 @ 09: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 @ 09: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 @ 11: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


Bob Majdak:
Apache access logging in JSON format
Jul 26, 2012 @ 11: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


Working with Dates and Times in PHP and MySQL
Mar 01, 2012 @ 08:51:47

On PHPMaster.com today there's a new tutorial by Sean Hudgston about working with dates and times via the PHP date functions and how they cooperate with dates/times from a MySQL database.

When working in any programming language, dealing with dates and time is often a trivial and simple task. That is, until time zones have to be supported. Fortunately, PHP has one of the most potent set of date/time tools that help you deal with all sorts of time-related issues: Unix timestamps, formatting dates for human consumption, displaying times with time zones, the difference between now and the second Tuesday of next month, etc. In this article I’ll introduce you to the basics of PHP’s time functions (time(), mktime(), and date()) and their object-oriented counterparts, and then take a look at MySQL dates and show you how to make them play nicely with PHP.

His examples include how to get the current Unix time, formatting dates/times, making timestamps and working with the more powerful DateTime objects. On the MySQL front, he shows the result of a normal date select, one using the "unix_timestamp" function and how to shift the result based on the user's timezone.

tagged: date time mysql datetime tutorial format unix timestamp


Seeing Triple!
Mar 03, 2011 @ 10:05:47

php|architect Magazine has officially announced the introduction of a new publishing format for their magazine. Readers are no longer tied to just PDF versions - they can now opt for ePUB editions of the magazine starting with this month's issue (and retroactively!).

Still, even though PDF was a less popular format in 2002 than it has become today, it was a much easier choice for us when the only way to read an electronic publication was to sit in front of a computer. The advent of portable e-book readers demanded that we consider other formats; as a result, about six months ago we started a skunkworks program, spearheaded by our very own Keith Casey, to create ePub and MOBI versions of our magazine.

Thanks to an ePUB document generator, the magazine can now be dynamically generated into three formats - the usual PDF file, an ePUB edition and a MOBI edition for the Kindle fans out there. All subscribers have been upgraded to this plan - the Premium subscription - and are able to start pulling down these editions today.

tagged: publishing format phparchitect pdf epub mobi