Anthony FerraraIntroducing Recki-CT (29.8.2014, 19:00 UTC)
Over 1.5 years ago, I introduced PHPPHP to the world. It was the first implementation of the PHP language written in PHP itself. But PHPPHP suffered from a few problems which relegated it to toy status (such as performance). Today, I get to introduce you to another implementation of PHP, written in PHP. But this one is no toy. This one... This one is fun...
Read more »
SitePoint PHPBuild REST Resources with Laravel (29.8.2014, 16:00 UTC)

In this part, we will begin to work with the REST interface. Creating a REST Api on Laravel isn’t very difficult. All we need to keep in mind is that we’re dealing with EmberJS and that we don’t want to write a new adapter from scratch. As usual, you can find the source code for this part on github.

Where to start?

That is a hard question. Ember has its own workflow and logic. If we begin to write our REST with that logic in mind we will save some time, we’ll have a nice architecture and something reusable. I think Ember has made a good choice with their REST architecture. Take a look at how Ember expects the data.

Let’s assume that we want to retrieve a user. Ember expects something like this:

  "user": {
    "firstName": "firstName",
    "lastName": "lastName"

If we want to retrieve a list of users, Ember would expect a json like this:

        "firstName": "firstPersonsName",
        "lastName": "lastname"
        "firstName": "secondPersonName",
        "lastName": "lastName"

The first one requires “user”, but the second one requires “users”. The second one is plural. Ember put in some rules for that too. If you don`t specify the plural yourself by using:

Ember.Inflector.inflector.irregular('formula', 'formulae');

EmberJs will make an assumption and request “formulas”. Sometimes, it’s nice that the framework itself provides such things, but on the other hand things can get out of control if you forget these details.

Before venturing deeper with Ember, a warning: Ember is difficult and powerful. Take the time to learn how it works.

If we complicate things a little bit and put some relations between the objects, for example we say that the user has some photos. How we would output that?

Continue reading %Build REST Resources with Laravel%

Simon HolywellFunctional Programming in PHP - The book (29.8.2014, 10:09 UTC)
Lorna MitchellCompiling PHP Extensions (29.8.2014, 08:40 UTC)

There are lots of reasons why you might like to compile your own PHP extensions. For me those reasons are usually:

  • The extension isn't available on pecl (e.g. uprofiler)
  • The extension is on pecl, but you need the newest version or a branch with a particular feature or fix in it, perhaps for testing
  • You are fixing an extension yourself (yay, we need more people like you!)

Related: If you followed my previous post on compiling PHP, be aware that in the php/bin/ folder there is a pecl binary that will install extensions correctly for whichever version of PHP it belongs to, so you may not need to read the rest of this post. However if you do, the paths follow on from the examples in that post.

I haven't seen a really approachable guide anywhere, we tend to speak of extensions in hushed tones, and actually it isn't particularly tricky so here is my quick how-to guide.

Start by grabbing the code you need. First we'll need to prepare it for compiling by explaining to it which version of PHP it is intended for.

For your default PHP install you can just run:

However for a version that isn't the default on your system, we can be a bit more specific with the paths to use:
phpize /path/to/toy/php/bin/phpize
./configure --with-php-config=/path/to/toy/php/bin/php-config

Either way, we're now all set to build and install our extension:
make install

This compiles the extension and places the binaries in the correct location (you may need to sudo make install if your user doesn't have write permission to the desired location). This process will tell you which extension file was created, such as

Check that PHP can load the extension by adding it on the command line and then viewing the modules list (beware that errors often appear at the top of this output and may scroll off screen!):

php -m

(remember to include the /path/to/toy/php for non-default installs)

You should see the extension listed in the output - you can now add it to your php.ini file or however you usually enable extensions and carry on with whatever it was you were doing!

Lorna is an independent web development consultant, author and trainer, available for work (interesting projects only). This post was originally published at LornaJane

SitePoint PHPPHP 5.6.0. Released (28.8.2014, 12:00 UTC)

PHP 5.6.0., considered a very important cornerstone by many, has been released today. We’ve talked about the changes this version brings in previous posts, and others have written about it, too.


Continue reading %PHP 5.6.0. Released%

PHP: Hypertext PreprocessorPHP 5.6.0 released (28.8.2014, 00:00 UTC)
The PHP Development Team announces the immediate availability of PHP 5.6.0. This new version comes with new features, some backward incompatible changes and many improvements. The main features of PHP 5.6.0 include:Constant scalar expressions.Variadic functions and argument unpacking using the ... operator.Exponentiation using the ** operator.Function and constant importing with the use keyword.phpdbg as an interactive integrated debugger SAPI.php://input is now reusable, and $HTTP_RAW_POST_DATA is deprecated.GMP objects now support operator overloading.File uploads larger than 2 gigabytes in size are now accepted.For a full list of new features, you may read the new features chapter of the migration guide.PHP 5.6.0 also introduces changes that affect compatibility:Array keys won't be overwritten when defining an array as a property of a class via an array literal.json_decode() is more strict in JSON syntax parsing.Stream wrappers now verify peer certificates and host names by default when using SSL/TLS.GMP resources are now objects.Mcrypt functions now require valid keys and IVs. For users upgrading from PHP 5.5, a full migration guide is available, detailing the changes between 5.5 and 5.6.0. For source downloads of PHP 5.6.0, please visit our downloads page. Windows binaries can be found on The full list of changes is available in the ChangeLog.
SitePoint PHPBuild a Database with Eloquent, Faker and Flysystem (27.8.2014, 16:00 UTC)

In this part, we will create the structure of the database. We will create the tables using migrations and seed the database using seeders. Also, you will learn how to grab some random images from LoremPixel and put them on the filesystem using Flysystem. You’ll also be adding some randomly generated data using the Faker library.
Much like with part 1, you can download this part’s code from github.

Install the libraries

The installation is very straightforward with Composer.

Require these projects:

"fzaninotto/faker": "1.5.*@dev",
"league/flysystem": "dev-feature/eventable-filesystem",
"dropbox/dropbox-sdk": "dev-master"

Execute composer update and everything will be installed and updated smoothly.

Build the structure of the database

Until now, we had just one migration file. We need two more for our application. One table that will keep the data for photos and another for categories. To find out more about Artisan and Migrations, see this post.

php artisan migrate:make create_photos_table
php artisan migrate:make create_categories_table

These two commands create two migration files. Let’s start filling them with some schema. First, we start with the photos migration file. Stop for a moment and think what we need for this one. Earlier, we installed the flysystem library.

The flysystem library can handle files on different locations: the local filesystem, Dropbox, AWS3, Rackspace Cloud Files and more. We need it for the local filesystem on our development environment and we have to use Dropbox for production. Why is that? Heroku is a Paas (Platform as a Service) so we don’t have full control of the filesystem. There are only two directories that can be writable: ./temp and ./logs. You can write files only using git for deployment, and not in any other folder. Why don`t we just use those two directories? Because they are temporary. If something goes wrong and the Dyno restarts, all the files in those directories are deleted. It’s not safe to work with those two directories.

If you want to know more about flysystem read this article on sitepoint by Lukas White or their documentation .

Continue reading %Build a Database with Eloquent, Faker and Flysystem%

Derick RethansNatural Language Sorting with MongoDB (27.8.2014, 08:33 UTC)

Natural Language Sorting with MongoDB

Arranging English words in order is simple—well, most of the time. You simply arrange them in alphabetical order. However sorting a set of German words, or French words with all their accents, or Chinese with their different characters is a lot harder than it looks. Sorting rules are specified through "locales", which determine how accents are sorted, in which order the letters are in and how to do case-insensitive sorts. There is a good set of those sorting rules available through CLDR, and there is a neat example to play with all kinds of sorting at ICU's demo site. If you really want to know how the algorithms work, have a look at the Unicode Consortium's report on the Unicode Collation Algorithm.

Right now, MongoDB does not support indexes or sorting on anything but Unicode Code Points. Basically, that means, that it can't sort anything but English. There is a long standing issue, SERVER-1920, that is at the top of the priority list, but is not scheduled to be added to a future release. I expect this to be addressed at a point in the near future. However, with some tricks there is a way to solve the sorting problem manually.

Many languages, have their own implementation of the Unicode Collation Algorithm, often implemented through ICU. PHP has an ICU based implementation as part of the intl extension. And the class to use is the Collator class.

The Collator class encapsulates the Collation Algorithm to allow you to sort an array of text yourself, but it also allows you extract the "sort key". By storing this generated sort key in a separate field in MongoDB, we can sort by locale—and even multiple locales.

Take for example the following array of words:

$words = [
        'bailey', 'boffey', 'böhm', 'brown', 'серге́й', 'сергій', 'swag',

Which we can turn into sort keys with a simple PHP script like:

$collator = new Collator( 'en' );
foreach ( $words as $word )
        $sortKey = $collator->getSortKey( $word );
        echo $word, ': ', bin2hex( $sortKey ), "\n";

We create a collator object for the en locale, which is generic English. When running the script, the output is (after formatting):

bailey: 2927373d2f57010a010a
boffey: 294331312f57010a010a
böhm:   2943353f01859d060109
brown:  294943534101090109
серге́й: 5cba34b41a346601828d05010b
сергій: 5cba34b41a6066010a010a
swag:   4b53273301080108
svere:  4b512f492f01090109

Those sort keys can be used to then sort the array of names. In PHP, that would be:

$collator->sort( $words );
print_r( $words );

Which returns the following list:

[0] => bailey
[1] => boffey
[2] => böhm
[3] => brown
[4] => svere
[5] => swag
[6] => серге́й
[7] => сергій

We can extend this script, to use multiple collations, and import each word including its sort keys into MongoDB.

Below, we define the words we want to sort on, and the collations we want to compare. They are in order: English, German with phone book sorting, Norwegian, Russian and two forms of Swedish: "default" and "standard":

<?php $words = [
        'bailey', 'boffey', 'böhm', 'brown', 'серге́й', 'сергій',
        'swag', 'svere'
$collations = [
        'en', 'de_DE@collation=phonebook', 'no', 'ru',
        'sv', 'sv@collation=standard',

Make the connection to MongoDB and clean out the collection:

$m = new MongoClient;
$d = $m->demo;
$c = $d->collate;

Create the Collator objects for each of our collations:

$collators = [];

foreach ( $collations as $collation )
        $c->createIndex( [ $collation => 1 ] );
        $collators[$collation] = new Collator( $collation );

Loop over all the words, and for each collation we have define, use the created Collator object to generate the sort key. We encode the sort key with bin2hex() because sort keys are binary data, and MongoDB requires UTF-8 for strings. M

Truncated by Planet PHP, read more at the original (another 4683 bytes)

Matthew Weier O'PhinneyDeployment with Zend Server (Part 1 of 8) (26.8.2014, 20:15 UTC)

I manage a number of websites running on Zend Server, Zend's PHP application platform. I've started accumulating a number of patterns and tricks that make the deployments more successful, and which also allow me to do more advanced things such as setting up recurring jobs for the application, clearing page caches, and more.

Yes, YOU can afford Zend Server

"But, wait, Zend Server is uber-expensive!" I hear some folks saying.

Well, yes and no.

With the release of Zend Server 7, Zend now offers a "Development Edition" that contains all the features I've covered here, and which runs $195. This makes it affordable for small shops and freelancers, but potentially out of the reach of individuals.

But there's another option, which I'm using, which is even more intriguing: Zend Server on the Amazon Web Services (AWS) Marketplace. On AWS, you can try out Zend Server free for 30 days. After that, you get charged a fee on top of your normal AWS EC2 usage. Depending on the EC2 instance you choose, this can run as low as ~$24/month (this is on the t1.micro, and that's the total per month for both AWS and Zend Server usage). That's cheaper than most VPS hosting or PaaS providers, and gives you a full license for Zend Server.

Considering Zend Server is available on almost every PaaS and IaaS offering available, this is a great way to try it out, as well as to setup staging and testing servers cheaply; you can then choose the provider you want based on its other features. For those of you running low traffic or small, personal or hobbyist sites, it's an inexpensive alternative to VPS hosting.

So... onwards with my first tip.

Tip 1: zf-deploy

My first trick is to use zf-deploy. This is a tool Enrico and I wrote when prepping Apigility for its initial stable release. It allows you to create deployment packages from your application, including zip, tarball, and ZPKs (Zend Server deployment packages). We designed it to simplify packaging Zend Framework 2 and Apigility applications, but with a small amount of work, it could likely be used for a greater variety of PHP applications.

zf-deploy takes the current state of your working directory, and clones it to a working path. It then runs Composer (though you can disable this), and strips out anything configured in your .gitignore file (again, you can disable this). From there, it creates your package.

One optional piece is that, when creating a ZPK, you can tell it which deployment.xml you want to use and/or specify a directory containing the deployment.xml and any install scripts you want to include in the package. This latter is incredibly useful, as you can use this to shape your deployment.

As an example, on my own website, I have a CLI job that will fetch my latest GitHub activity. I can invoke that in my post_stage.php script:

if (! chdir(getenv('ZS_APPLICATION_BASE_DIR'))) {
  throw new Exception('Unable to change to application directory');

$php = '/usr/local/zend/bin/php';

$command = $php . ' public/index.php githubfeed fetch';
echo "\nExecuting `$command`\n";

One task I always do is make sure my application data directory is writable by the web server. This next line builds on the above, in that it assumes you've changed to your application directory first:

$command = 'chmod -R a+rwX ./data';
echo "\nExecuting `$command`\n";

Yes, PHP has a built-in for chmod, but it doesn't act recursively.

For ZF2 and Apigility applications, zf-deploy also allows you to specify a directory that contains the *local.php config scripts for your config/autoload/ directory, allowing you to merge in configuration specific for the deployment environment. This is a fantastic capability, as I can keep any private configuration separate from my main repository.

Deployment now becomes:

$ vendor/bin/zfdeploy.php --configs=../ --zpk=zpk

and I now have a ZPK ready to push to Zend Server.

In sum: zf-deploy simplifies ZPK creation, and allows you to add deployment scripts that let you perform other tasks on the server.

Next time...

I've got a total of 8 tips queued up, including this one, and will be publishing on Tuesdays and Thursdays; I'll update each post to link to the others in the series. Next tip: creating scheduled Job Queue jobs, à la cronjobs.

Other articles in the series

I will update this post to link to each article as it releases.
labs @ Qandidate.comBringing CQRS and Event Sourcing to PHP. Open sourcing Broadway! (26.8.2014, 14:00 UTC)

Last week we open sourced our toggle library, API and GUI, see our announcements here and here. Today we open source Broadway! Broadway is a project providing infrastructure and testing helpers for creating CQRS and event sourced applications. Broadway tries hard to not get in your way. The project contains several loosely coupled components that can be used together to provide a full CQRS\ES experience.

∞ labs @ Permalink

LinksRSS 0.92   RDF 1.
Atom Feed   100% Popoon
PHP5 powered   PEAR
ButtonsPlanet PHP   Planet PHP
Planet PHP