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

SitePoint PHP Blog:
Randomness in PHP – Do You Feel Lucky?
Oct 29, 2015 @ 13:52:24

The SitePoint PHP blog has a post from author Nicola Pietroluongo talking about randomness in PHP. In the tutorial he talks about randomness, how it relates to cryptography and what's coming in PHP 7 to help.

This article analyzes problems related to random number generation used for cryptography purposes. PHP 5 does not provide an easy mechanism for generating cryptographically strong random numbers, while PHP 7 solves this by introducing a couple of CSPRNG functions.

He starts off by talking about what a CSPRNG (cryptographically secure pseudorandom number generator) is and some of the things it could be used for. He then moves on to the functionality coming in PHP 7 with the addition of the random_* functions for getting random bytes and random integer values. He talks briefly about what's going on "behind the scenes" of the generation and provides a simple code example with a randomized "dice roll" and the resulting numbers. He ends the post mentioning the random_compat library that can be installed for pre-PHP 7 applications that provides the same functionality just without those two functions defined.

tagged: random generation csprng number generator tutorial php7 php5 randomcompat

Link: http://www.sitepoint.com/well-do-ya-punk/

SitePoint PHP Blog:
Memory Performance Boosts with Generators and Nikic/Iter
Oct 20, 2015 @ 09:31:24

On the SitePoint PHP blog there's a tutorial posted showing you how to get some performance gains in your PHP applications using the "Iter" library from Nikita Popov.

First came arrays. Then we gained the ability to define our own array-like things (called iterators). But since PHP 5.5, we can rapidly create iterator-like structures called generators. These appear as functions, but we can use them as iterators. They give us a simple syntax for what are essentially interruptible, repeatable functions. They’re wonderful!

And we’re going to look at a few areas in which we can use them. We’re also going to discover a few problems to be aware of when using them. Finally, we’ll study a brilliant library, created by the talented Nikita Popov.

They start with a common problem: loading information line-by-line from a CSV file. They do some filtering and merging of the values but point our a major flaw - large files. These would drag down performance quite a bit and generators might just make for a good solution. He shows a simple "read CSV" generator to get the lines in the file while also reducing the memory needed. Unfortunately the array_map/array_filter methods he was using for sorting don't work with generators. The nikic/iter helps fix this. Code examples are included showing it in use performing the same operations as before. He ends the post with a few other "fun things" including array flattening, slicing and rewinding generators.

tagged: memory performance boost generator nikic iter library tutorial

Link: http://www.sitepoint.com/memory-performance-boosts-with-generators-and-nikiciter/

Paragon Initiative:
Coming to WordPress 4.4: CSPRNG
Oct 12, 2015 @ 12:52:42

The Paragon Initiative blog has a post from Scott Arciszewski about a new feature coming to upcoming WordPress versions - the use of a cryptographically security random number generator starting in version 4.4.0.

At Paragon Initiative Enterprises, we believe that security should be the default state of affairs, not something only in the reach of security experts. That is why [...] our team spends a great deal of time working to improve the security of popular free and open source software.

Today, we're pleased to announce an exciting security enhancement coming to WordPress in the next major version. Starting in 4.4.0, wp_rand() is cryptographically secure on all platforms.

He walks the reader through the "road" that's lead to the introduction of this support and the work he did in the past to help push the project (and others) towards it. Given that the WordPress project has a lot of emphasis on backwards compatibility, effort need to be put into a method that would work across new and old PHP versions. The random_compat library was created and was adopted not only by WordPress but also by several other major PHP projects.

Our part in this long and crazy journey has reached its end. In the course of fixing the same flaw in two distinct projects, the PHP community banded together to identify and expunge a bug in the PHP core, create a new feature in PHP 7, and in some small way helped to secure the CMS that powers more than 20% of websites on the Internet.
tagged: wordpress csprng random number generator cryptography security

Link: https://paragonie.com/blog/2015/10/coming-wordpress-4-4-csprng

Exporting Drupal Nodes with PHP and Drush
Oct 06, 2015 @ 11:09:11

The php[architect] site has posted a tutorial showing you how to export Drupal nodes with Drush and a bit of regular PHP. Drush is a command line tool that makes working with Drupal outside of the interface simpler and easier to automate.

Drupal 8 development has shown that PHP itself, and the wider PHP community, already provides ways to solve common tasks. In this post, I’ll show you some core PHP functionality that you may not be aware of; pulling in packages and libraries via Composer is a topic for another day.

The tutorial walks through a more real-world situation of needing to export a CSV file that shows a list of nodes added to the site after a specific date. He points out some of the benefits of doing it the Drush way and starts in on the code/configuration you need to set the system up. He shows how to create the Drush command itself and update it with a method to export the newest nodes (after validating the date provided). He makes use of a SplFileObject to output the results from the EntityFieldQuery query out into to the CSV file. He makes use of PHP's generators functionality to only fetch the records a few at a time. Finally he includes the command to execute the export, defining the date to query the node set and how to push that output to a file.

tagged: export drupal node drush commmandline csv output query generator

Link: https://www.phparch.com/2015/10/exporting-drupal-nodes-with-php-and-drush/

Evert Pot:
Save memory by switching to generators
Aug 11, 2015 @ 09:45:51

Evert Pot has a post to his site showing you to conserve memory with generators in your PHP scripts. Generators are a language feature that allows you to generate/manipulate data like an iterator without needing to pre-generate the array beforehand.

Since the release of PHP 5.5, we now have access to generators. Generators are a pretty cool language feature, and can allow you to save quite a bit of memory if they are used in the right places. [...] It's not uncommon in complex applications for the result of a function like our [example] to be passed to multiple functions that mangle or modify the data further. Each of these functions tend to have a (foreach) loop and will grow in memory usage as the amount of data goes up.

He uses a common example of fetching a set of articles from a database to show how memory consumption could get huge when a large number of articles are involved. He rewrites the example using generators instead, making use of the yield functionality to only fetch one record at a time and map it to the object structure. He also includes a few things to watch out for when using generators including the different return value of the method (iterator, not an array). He also points out an issue where the array_* functions will no work on iterators so you'd need to convert it back to an array before use.

tagged: memory generator switch example records yield

Link: http://evertpot.com/switching-to-generators/

SitePoint WordPress Blog:
The WordPress Plugin Boilerplate Part 2: Developing a Plugin
Jun 30, 2015 @ 10:07:50

The SitePoint WordPress blog has posted the second part of their series covering the creation of a WordPress plugin with the help of the WordPress Plugin Boilerplate. In this latest article they build on the first part of the series and start in on the actual plugin development.

In the first part of my series, an introduction to the WordPress Plugin Boilerplate, we looked at how the code is organised within the Boilerplate. To continue with this series, we’ll apply what we’ve learnt previously to build a real working plugin. We are going to take a look at how quickly we can get our plugin up and running using the Boilerplate code, with as little work as possible. This article will focus on creating and activating the plugin, as well as developing the admin facing functionality of the plugin.

They show you how to create a simple "time since posted" plugin with a few customizations available. They show how to use the Boilerplate generator to set up the basic plugin file structure and installing it on your WordPress application. From there they show you how to create a simple "Settings" page for the plugin and making it work via the functionality Boilerplate offers. The post then shows how to register the plugin, populate the options page and saving the changes the user makes.

tagged: wordpress boilerplate plugin generator tutorial development lastposted

Link: http://www.sitepoint.com/wordpress-plugin-boilerplate-part-2-developing-a-plugin/

Engine Yard Blog:
What to Expect When You're Expecting: PHP 7, Part 2
Apr 08, 2015 @ 11:07:08

The Engine Yard blog has posted the second part of Davey Shafik's "What to Expect with You're Expecting: PHP7" series. In this new post he gets into the details of a few more of the upcoming PHP7 features including generator improvements and engine exceptions.

As you probably already know, PHP 7 is a thing, and it’s coming this year! Which makes this as good a time as any to go over what’s new and improved. In the first part of this series, we looked at the some of the most important inconsistency fixes coming up in PHP 7 as well as two of the biggest new features. In this post, we take a look another six big features to land in PHP 7 that you’ll want to know about.

The features he talks about this time are:

  • Unicode Codepoint Escape Syntax
  • Null Coalesce Operator
  • Bind Closure on Call
  • Group Use Declarations
  • Generator return expressions and delegation
  • Engine Exceptions

He also includes three things you can do to help/get prepared for this upcoming release including testing your code on a PHP7 VM or help out with writing tests and documentation for PHP and its extensions.

tagged: engineyard php7 feature list major unicode coalesceoperator bindclosure groupuse generator engineexception

Link: https://blog.engineyard.com/2015/what-to-expect-php-7-2

Christopher Pitt:
Co-operative PHP Multitasking
Mar 30, 2015 @ 12:47:41

Christopher Pitt has posted a new article on Medium.com about when an "array is like an adventure" when in the context of co-operative PHP multitasking. In it he shows how to make code work asynchronously with out the use of extensions, only generators.

Last week I got the opportunity to share recent work with my colleagues, at SilverStripe. I was going to present Async PHP today, but since I covered ReactPHP last week; I decided to talk about something slightly different. So here’s a post about cooperative multitasking.

He starts with some basic arrays and other things that act like them and can be iterated through (Traversable). He talks about implementing custom iterators to act the same way and the use of IteratorAggregate to "cheat" a bit when making them. The he gets into generators, showing how they can be used to iterate similarly. He shows how it's possible to send data to a generator, throwing exceptions inside them and the use of "coroutines" to create asynchronous code. He builds up a queue system with this method and shows how they execute with some simple echo output. He also shows the use of RecoilPHP, another coroutine-based library, to replace the main kernel for a ReactPHP script. He also mentions IcicleIO as another option.

tagged: cooperative multitasking asynchronous code coroutine generator

Link: https://medium.com/@assertchris/co-operative-php-multitasking-ce4ef52858a0

Paul Jones:
Bookdown: DocBook-Like HTML Output From Markdown
Mar 05, 2015 @ 10:49:27

Paul Jones has posted about a new tool he's worked up specifically for authors looking to write using Markdown and wanting it to generate out like DocBook results. His tool, Bookdown, uses Markdown and JSON files instead of XML configurations.

Yes, I know, there’s a ton of static site generators for PHP out there already [...but they're] not DocBook-like documentation. By “DocBook-like”, I mean (among other things) numbered headers, auto-generated tables-of-contents on their own pages, hierarchical multi-page presentation, and the next/previous/up linking at the top and bottom of pages.

[...] So: Bookdown. This scratches my particular itch, with very few dependencies. Bookdown, although it can be used as a site generator, is only incidentally a site generator. What it really is is a page generator, with the idea that you can integrate the pages into any other site you want.

The library is separate from the project and is written to use a dependency injection methodology to keep things decoupled and well-structured. If this sounds interesting either for personal use or if you'd like to check out the code, head over to the project site for more information.

tagged: markdown bookdown library project docbook output static generator

Link: http://paul-m-jones.com/archives/6088

Edd Mann:
Implementing Streams in PHP
Jan 16, 2015 @ 10:09:22

Edd Mann has a new post today looking at implementing streams in your PHP applications. In this case we're not talking about the streams built into PHP but the concept of a source of information that only produces the next item when requested (aka "lazy loading").

Typically, when we think about a list of elements we assume there is both a start and finite end. In this example the list has been precomputed and stored for subsequent traversal and transformation. If instead, we replaced the finite ending with a promise to return the next element in the sequence, we would have the architecture to provide infinite lists. Not only would these lists be capable of generating infinite elements, but they would also be lazy, only producing the next element in the sequence when absolutely required. This concept is called a Stream, commonly also referred to as a lazy list, and is a foundational concept in languages such as Haskell.

He talks about how streams of data should be interacted with differently than a finite list of data and the promises they're based on to provide the right data. He shows two different approaches to implementing a an object to stream data from - a class-based method and one that uses generators. Sample code is provided for each with the generator approach being a bit shorter as they're designed to lazy load items as requested.

tagged: stream data lazyload generator class iterator tutorial

Link: http://eddmann.com/posts/implementing-streams-in-php/