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

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/

How to use the “yield” keyword in PHP 5.5 and up
May 23, 2014 @ 12:09:47

In a recent post to the LeaseWebLabs blog Maurits van der Schee looks at the use of the "yield" keyword in PHP 5.5 to work with generators. A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values but it yields values one at a time.

The concept of generators is not new. The “yield” keyword exists in other programming languages as well. As far as I know C#, Ruby, Python, and JavaScript have this keyword. The first usage that comes to mind for me is when I want to read a big text file line-by-line (for instance a log file). Instead of reading the whole text file into RAM you can use an iterator and still have a simple program flow containing a “foreach” loop that iterates over all the lines.

He includes a few code examples showing a class that can read in data from a file in chunks and output the lines as they're extracted (versus using something like file). He also talks about a small performance comparison in working with the file pointer, fread over fgets. He even makes a simple benchmark script to compare the overall time and memory consumption of the fetching of different byte "chunks" from the file.

tagged: yield generator file read fread fgets memory time benchmark

Link: http://www.leaseweblabs.com/2014/05/how-to-use-yield-keyword-php

Generators in PHP
Aug 06, 2013 @ 12:25:50

On PHPMaster.com a tutorial has been posted talking about one of the newer features in PHP - generators. In the tutorial Stefan Froelich walks you through how they work and a few examples of their use.

Generators in PHP If you’ve followed my previous posts about iterators then you’ll know that iteration is an important programming concept, but implementing the required interfaces to create an iterable object can be a hassle at best because of the amount of boilerplate code that is required. With the release of PHP 5.5, we finally have generators!

He starts with a more practical example - pulling lines from a file, one at a time, without the overhead of having to read in the entire file at once. He also includes an example of returning the keys from the generator (not just the value) and injecting values with the "send" method.

tagged: generator tutorial introduction example yield inject keys

Link: http://phpmaster.com/generators-in-php

Blake Gardner:
Practical usage of PHP 5.5 generators: yield keyword
Jun 24, 2013 @ 11:54:42

With the release of PHP 5.5 came a whole group of new features, including the "yield" keyword for better handling of values in iteration. Blake Gardner has posted a practical example of its use to his site today.

The key to understating the way the yield works verses a normal function is that rather than generating all of your data and returning the final array when it’s done; you yield the value as it’s generated. The state of the generator function is saved after you yield and then its state is restored when called again so the iteration can continue.

He shows a basic use of "yield" in a simple foreach of 1000000 values. In the first example, memory is exhausted and the second yields the values as they come, reducing the overhead significantly. The "range_yield" function returns them as the "for" loop generates them.

tagged: yield feature practical use tutorial generator

Link: http://blakegardner.co/2013/06/24/practical-usage-of-php-5-5-generators-yield-keyword

Lorna Mitchell:
Simplest PHP Generator Example
May 23, 2013 @ 10:31:02

On her blog Lorna Mitchell has posted an example of a basic generator written in PHP, a feature of the upcoming PHP version 5.5.

I really like the generators feature that's arriving in PHP 5.5, and since we're now into release candidate releases, it's actually not that far away. I've been speaking on this topic and I thought I'd share my trivially simple code example from my slides.

She includes an example of a very basic generator using the new "yield" keyword and how to implement it in a simple foreach loop. There's also a little talk about when is a good time to use generators in your applications (two examples: complex number calculation and working with large data sets a chunk at a time). For more information on how these generators will work, check out this page in the PHP manual.

tagged: generator simple example introduction manual

Link: http://www.lornajane.net/posts/2013/simplest-php-generator-example