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

Edd Mann:
Mapping, Filtering and Reducing in PHP
Mar 03, 2016 @ 09:28:27

Edd Mann has a post to his site talking about the use of things like "map" and "reduce" in his own development and how to use it in PHP to help reduce data sets and filter them based on certain criteria.

Over the past couple of years I have transitioned from boilerplate-heavy, imperative code, to begin using the more intuitive map, filter and reduce higher-order functions. In this article I hope to highlight the transformation that occurs, along with simplification, when moving away from the imperative and onto the declarative. We shall be performing a simple process which transforms a collection of user entries into a filtered collection of their names. Although trivial in nature, it is a great place to highlight the power of the paradigm shift.

He starts with a simple array of data: a set of users with their respective IDs and names. He shows a typical approach, using a method that loops through the data to find only the "name" values. He then shows an alternative that makes use of PHP's own array_filter and array_reduce functions to perform the same operation with just a bit more internal handling.

tagged: filter reduce array arrayfilter arrayreduce example simple

Link: http://eddmann.com/posts/mapping-filtering-and-reducing-in-php/

Ibuildings Blog:
Programming Guidelines - Part 1: Reducing Complexity
Jan 21, 2016 @ 11:53:08

On the Ibuildings blog Matthias Noback has kicked off a series that wants to help PHP developers reduce the complexity of their applications. In part one he shares some general tips along with code snippets illustrating the change.

PHP is pretty much a freestyle programming language. It's dynamic and quite forgiving towards the programmer. As a PHP developer you therefore need a lot of discipline to get your code right. Over the years I've read many programming books and discussed code style with many fellow developers. I can't remember which rules come from which book or person, but this article (and the following ones) reflect what I see as some of the most helpful rules for delivering better code: code that is future-proof, because it can be read and understood quite well. Fellow developers can reason about it with certainty, quickly spot problems, and easily use it in other parts of a code base.

The rest of the article is broken up into several changes you can make to reduce complex code including:

  • Reduce the number of branches in a function body
  • Create small logical units
  • Using single (variable) types
  • Making expressions more readable

He ends this first post in the series with a mention of a few other books to read up on around the subject of "clean" and less complex code.

tagged: reduce complexity programming guideline series part1

Link: https://www.ibuildings.nl/blog/2016/01/programming-guidelines-php-developers-part-1-reducing-complexity

Mark Baker:
A Functional Guide to Cat Herding with PHP Generators
Jan 19, 2016 @ 10:05:13

In this post to his blog Mark Baker looks at a feature added in PHP 5.5 - generators - and how to use them with some of the array handling functionality PHP provides.

When working with arrays in PHP, three of the most useful functions available to us are array_map(), array_filter() and array_reduce().

[...] However, these functions only work with standard PHP arrays; so if we are using Generators as a data source instead of an array, then we can’t take advantage of the functionality that they provide. Fortunately, it’s very easy to emulate that functionality and apply it to Generators (and also to other Traversable objects like SPL Iterators), giving us access to all of the flexibility and power that mapping, filtering and reducing can offer.

He starts with a more "real world" example of using a generator in a handler for GPX files, XML files storing GPS data. He gives an example of the typical file contents and shows a simple generator script (class) that he uses to grab chunks of the file at a time instead of reading it all in and parsing it from there. He then uses this generator along with a bit of extra handling to mimic array filtering, transformation and reducing the data being returned.

tagged: functional generator tutorial array filter reduce transformation

Link: http://markbakeruk.net/2016/01/19/a-functional-guide-to-cat-herding-with-php-generators/

PHPRiot.com:
Reducing a Map Path Using Douglas-Peucker Algorithm
Apr 29, 2011 @ 12:40:42

On PHPRiot.com there's a new tutorial showing you how to use the Douglas-Peucker algorithm to make the pathing on your map the simplest possible (the fewer points the better) having serious benefits a wide range of users, most notably those on mobile devices.

When drawing a path on a map (for instance, the directions from point A to point B) it is important to consider the limitations of the device you're drawing the path on. In this article, I will show you how to reduce the number of points in a path so the path can be displayed with minimal loss of quality on devices such as iPhone or Android-powered devices that may struggle with an extremely large set of points.

Using the GTFS service's data, he's been creating maps for an iPhone application. Naturally, a path with 700 points would take a lot of resources to render. Using the Douglas-Peucker algorithm he can reduce it down to a much more manageable 70 points. He explains the algorithm briefly and hows how to implement it in PHP with three classes - ShapePoint, Shape and ShapeReducer. The resulting reduced dataset is then passed directly over to a Google Maps for plotting.

tagged: douglaspeucker algorithm reduce tutorial map point path

Link:

Chris Jones' Blog:
Reducing diskspace of your Oracle Instant Client install
Apr 09, 2009 @ 10:29:16

Chris Jones has a recommendation for those compiling Oracle support into PHP - there's a simple way to reduce the amount of disk space that your Instant Client install uses - a "only what's needed" setup.

Most PHP OCI8 users link PHP with the Oracle Instant Client (IC) libraries that handle the underlying "client" (i.e PHP OCI8 extension) communication with the database. IC is relatively small for the features it gives, and is very easy to install. A ZIP or RPM download unpacks a few libraries. [...] Although IC isn't particularly big - and it's getting relatively smaller all the time - there are two ways to reduce its size.

His two recommendations are to use the lite client version instead of the full-blown one (only has common character sets) or you can just remove the JDBC/ODBC/OCCI libraries from the full install. He lists out the files that are actually needed by the OCI8 extension to compile correctly.

tagged: reduce disk space oracle install instant client oci8 compile

Link:

Ryan Grove's Blog:
Minify combines, minifies &caches JavaScript & CSS files to speed up page loads
May 04, 2007 @ 16:52:00

This new entry on Ryan Grove's blog today points out a handy little tool written in PHP that can be used to (somewhat) reduce the time it takes for your pages to download.

Minify is a PHP library that attempts to fix this problem by combining multiple CSS or JavaScript files into one download. By default, it also removes comments and unnecessary whitespace to decrease the amount of data that must be sent to the browser. Most importantly, it does all of this on the fly and requires only a few simple changes to your existing web pages.

He gives a before and after example of the things it can do - mainly combining multiple tags (like link and script) and their information into one. Check out its page on the Google Code site for more information.

tagged: css javascript combine reduce load time css javascript combine reduce load time

Link:

Ryan Grove's Blog:
Minify combines, minifies &caches JavaScript & CSS files to speed up page loads
May 04, 2007 @ 16:52:00

This new entry on Ryan Grove's blog today points out a handy little tool written in PHP that can be used to (somewhat) reduce the time it takes for your pages to download.

Minify is a PHP library that attempts to fix this problem by combining multiple CSS or JavaScript files into one download. By default, it also removes comments and unnecessary whitespace to decrease the amount of data that must be sent to the browser. Most importantly, it does all of this on the fly and requires only a few simple changes to your existing web pages.

He gives a before and after example of the things it can do - mainly combining multiple tags (like link and script) and their information into one. Check out its page on the Google Code site for more information.

tagged: css javascript combine reduce load time css javascript combine reduce load time

Link: