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

Edd Mann:
Developing a Resizable-Indexed Array as a PHP Extension with Joe Watkins
Aug 31, 2015 @ 12:43:11

Edd Mann has a post to his site sharing a new screencast about developing PHP extensions by Joe Watkins, specifically about using a resizable-indexed array.

Following on from our first screencast, which touched upon how to setup a PHP extension development environment and creating a simple ‘array_sum’ like function. We now further this topic by implementing a resizable-indexed array class which supplies very similar functionality to that of the SplFixedArray class. Throughout this discussion we look into creating a custom Zend object class, using this to invoke an implemented data-structure located in another file, implementing internal interfaces, object handlers and the importance of PHP extension tests.

You can watch the video either through the in-page video player or directly on YouTube if you'd like a bit more control over the sizing. You can find the resulting code for the extension in this repository on Joe's account.

tagged: resizable indexed array extension tutorial screencast youtube threedevsandamaybe

Link: http://eddmann.com/posts/developing-a-resizable-indexed-array-as-a-php-extension-with-joe-watkins/

PHPClasses.org:
How to Create a PHP C Extension to Manipulate Arrays Part 2: Adding ArrayAccess and
Aug 13, 2015 @ 12:33:04

Dmitry Mamontov has posted the second part of his "How to Create a PHP C Extension to Manipulate Arrays" series on PHPClasses, building on part one and adding in the ArrayAccess and Traversable interface functionality.

In the first part of this article we learned how to create an extension for PHP written in C to create a class that works like arrays. However, to make the class objects really behave as arrays you need to implement certain interfaces in the class.

Read this article to learn how to make a PHP class defined by a C extension implement ArrayAccess and Traversable interfaces, as well understand how to solve problems that you may encounter that can make your extension slower than you expect.

He takes the class he defined in part one and walks you through the addition of the two interfaces. He shows you where they're defined in the PHP source, what the code looks like and how they integrate with the class. He also shows you how to customize the object class handlers, making it possible to use the custom class (object) as an array. Adding Traversable is easier, adding an iterator return method that allows for the data internal to the class to be iterated through.

tagged: phpclasses series part2 extension class array manipulate arrayaccess traversable

Link: http://www.phpclasses.org/blog/post/306-How-to-Create-a-PHP-C-Extension-to-Manipulate-Arrays-Part-2-Adding-ArrayAccess-and-Traversable-interfaces.html

PHPClasses.org:
How to Create a PHP C Extension to Manipulate Arrays Part 1: Basic Array Class Exten
Aug 11, 2015 @ 10:27:24

Dmitry Mamontov has posted a the first part of a series looking at manipulating arrays in the PHP source and enhancing performance for certain handing as a PHP extension.

In PHP projects, arrays are used every where because they are useful and flexible to store all sorts of data structures. However, when you need to maximize the performance the manipulation of arrays for specific purposes, you can achieve great gains if you implement a PHP extension written in the C language. Read this tutorial to learn how to build your own basic array manipulation extension in C.

He covers all the steps you'll need to get start building the extension, introduces a few key concepts and starts on the code for the extension:

  • Building PHP from the Source
  • Building a PHP Extension (overview)
  • Brief Introduction to zval and Functions
  • Defining a Class in Our Extension
  • D for Dynamic (working with dynamic array values)

The C code needed is included through out the post. The next part in the series will build on this and show how to implement interfaces like ArrayAccess and Traversable.

tagged: extension array manipulate part1 series introduction source

Link: http://www.phpclasses.org/blog/post/304-How-to-Create-a-PHP-C-Extension-to-Manipulate-Arrays--Part-1-Basic-Array-Class-Extension.html

Simon Holywell:
International PHP dates with intl
Jul 22, 2015 @ 12:55:33

Following up on his previous post about date/time handling and localization, Simon Holywell has posted an update with information about using the "Intl" extension for PHP to get it working with the more powerful PHP DateTime handling.

I wrote about localising dates (and other data) in a recent blog post, but unfortunately there were some shortcomings where time zones were concerned. As I alluded to in that post there is a way around this via the Intl extension that exposes a simple API to format DateTime instances. Thankfully this follow up post will be quite short as the setup is very simple for those of you on Ubuntu/Debian you can use the repositories.

He includes the commands you'll need to install the extension (via apt-get), compile it via the "pecl" command and update your php.ini file to enable it. He also includes some code examples showing how to use the IntlDateFormatter handling to work with dates, formats and calendars.

tagged: international datetime intl extension tutorial aptget pecl

Link: https://www.simonholywell.com/post/2015/07/international-php-dates-with-intl/

Lorna Mitchell:
PHP 7 Benchmarks
Jul 06, 2015 @ 12:42:55

Lorna Mitchell has posted some preliminary PHP7 benchmarks from the current alpha release (alpha2). Good news - it's fast....very fast.

If you know anything at all about PHP7, you probably know it's fast. But did you know how fast? The alpha is out and looks very robust, so I decided I would create a new set of benchmarks to include it. Graphs first, disclaimers later :)

This graph shows the time it takes for each version of PHP to perform the same task, on average, with oldest PHP on the left and moving forward in time. [..] The benchmark is the Zend/bench.php that lives in the PHP sourcecode (run ten times for each version of PHP using the php7dev VM on an average laptop, and then the mean result for each version calculated). The script runs through a series of taxing algorithms, giving a sense of how quickly a series of computational instructions can be executed.

She also talks briefly about how this can effect more real-world applications, how realistic it is to upgrade from older installs (much less painful on 5.5 or 5.6) and some things you can do to help improve PHP7 for everyone. This includes testing, working on bugs and adding extensions to this list to ensure they're made PHP7 compatible.

tagged: php7 benchmark fast realworld help testing bugfix extension

Link: http://www.lornajane.net/posts/2015/php-7-benchmarks

Sammy Powers:
Contributing to the PHP Manual
Jun 19, 2015 @ 13:23:27

If you've wanted to contribute something back to PHP but aren't familiar with C (or don't feel comfortable enough with it) Sammy Powers offers another solution. In his latest post he shows you how to contribute to the PHP documentation and update the manual for new features, missing information or fixes to current code examples.

If you've been wanting to contribute to PHP internals, starting with the documentation can be a great entry point; especially because it doesn't require dusting off those old C books from college. But knowing where to start can be tricky since information on how to contribute to the docs is scattered across the internet. This article is a step-by-step guide of how to contribute documentation to the PHP manual.

He starts with the "quick and dirty" way of editing the manual through the edit.php.net site, but points out that it's really only useful for smaller changes, not large documentation updates. The rest of the post shows you how to set up the documentation locally and generate the results to validate your changes. He talks some about the DocBook format they're written in, the build process with the PhD (PHP docs generator) and running the php.net test suite against the changes. This ensures that nothing else has broken on the site in the process.

He shows you where to make your changes, how to generate it from either a skeleton or using the docgen script and submitting the changes back to the repository. There's also a few other random changes to make before committing the files back via SVN and pushing them back upstream. He ends the post talking about the GoPHP7-ext project and how to find extensions that are missing documentation or where it's incomplete (easy thanks to an included "check-missing-docs" file included in the repository).

tagged: contribute documentation phpnet manual extension gophp7ext docgen tutorial

Link: https://www.sammyk.me/how-to-contribute-to-php-documentation

Giorgio Sironi:
Property-based testing primer
Jun 19, 2015 @ 12:15:29

Giorgio Sironi has a new post to his site today talking about a method (and tool) around a different sort of testing practice: property-based testing. The difference is that, instead of hard-coding values and evaluating them post-processing, you're generating random values and ensuring they match against a set of properties, not values.

I'm a great advocate of automated testing and of finding out your code does not work on your machine, 30 seconds after having written it, instead of in production after it has caused a monetary loss and some repair work to be performed. [...] However, classic test suites written with xUnit and BDD styles have some scaling problems they hit when you want to exercise more than some happy paths. [...] Property-based testing is an approach to testing coming from the functional programming world.

He helps to make the point a bit more clear with an example of testing the "sort" function and its results. He talks about how to test it using normal data, empty data, etc. but notes that this kind of testing can become "boring and error-prone". Instead he proposes the property-based testing of the results. He generates random values to feed into the method and checks to ensure the results are sorting by comparing them to each other. He expands this with a bit more complex example, showing how to test some DateTime handling and evaluating the result with logic in a closure. To help make this kind of testing easier, he's created a library (Eris) that extends PHPUnit and provides the methods seen in his examples.

tagged: property testing unittest phpunit extension random datetime sort eris

Link: http://www.giorgiosironi.com/2015/06/property-based-testing-primer.html

SitePoint PHP Blog:
Building a Custom Twig Filter the TDD Way
Jun 08, 2015 @ 13:40:18

The SitePoint PHP blog has a new tutorial posted showing you how to create your own Twig template following a TDD (test-driven development) mentality.

Twig is a powerful, yet easy to master template engine. It is also my personal favorite as all my web development is based on either Symfony or Silex. Apart from its core syntax ({{ ... }} and {% ... %}), Twig has built-in support for various filters. A filter is like a “converter”. It receives certain original data (a string, a number, a date, etc) and by applying a conversion, outputs the data in a new form (as a string, a number, a date, etc).

He starts with a brief introduction to what filters in Twig are and some simple ways to use them. From there he gets into building a custom filter, starting with the tests first (hence the test-driven design). He walks you through the creation of a filter that turns times into relative strings, like "Just now" or "Within an hour". He shows how to make the extension classes and integrate it into a Symfony application.

tagged: twig filter tutorial custom timediff extension tdd testdriven development

Link: http://www.sitepoint.com/building-custom-twig-filter-tdd-way/

Frank de Jonge:
Rendering ReactJS templates server-side
May 21, 2015 @ 09:17:50

Frank de Jonge has posted a tutorial to his site showing how you can render React.js templates server-side in PHP. He makes use of the V8JS extension to execute Javascript inside of PHP and echo out the rendered result.

The last couple of months I've been working with ReactJS quite extensively. It's been a very rewarding and insightful journey. There is, however, one part that kept coming back to me: server-side rendering. How on earth am I going to use ReactJS when I want to render my templates on the server? So, I sat down and looked at the possibilities.

He suggests two options, running a small Node application or using the V8JS extension, and opts for trying the second option to meet his needs. He talks about the "why" of rendering server-side JS and gives a brief introduction to V8JS and the workflow he'll follow to use it. He helps you get this library via Composer to make working with it easier and provides an example of how to use it. After trying out this method, he then goes back to option #1, the small Node application (what he ended up choosing). He walks through the setup of this application, showing how to set it up inside a Lumen application and using Express to output the generated templates and data. He then hooks this into the PHP application via a simple HTTP client grabbing the results and pushing them back out to the page.

tagged: reactjs template serverside nodejs v8js extension http lumen

Link: http://blog.frankdejonge.nl/rendering-reactjs-templates-server-side/

Hannes Magnusson:
Next Generation MongoDB Driver for PHP!
Apr 15, 2015 @ 11:41:50

Hannes Magnusson has a new post to his site talking about the new update to the MongoDB driver for PHP and its focus on simplicity.

For the past few months I've been working on a "next-gen" MongoDB driver for PHP -- codename "phongo". The aim was to build a new PHP extension ontop of the mongoc and libbson libraries to reduce maintenance of the extension itself and focus more on providing the ecosystem with improved support and libraries.

The new driver is available on PECL (called "mongodb", surprisingly enough). It doesn't include any of the bells and whistles found in the previous "mongo" driver. It doesn't include any `group` or `count` command helpers, and you won't find any Collection or Database objects; however, it really doesn't need any of these things.

He talks about the three basic things it can do: execute a command, a write or a query to locate records. He also answers the question many developers have about this shift to simplicity and provides a link to a PHP library to make porting over existing MongoDB handling simpler.

tagged: mongodb driver pecl extension language simplicity version release

Link: http://bjori.blogspot.com/2015/04/next-gen-mongodb-driver.html