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

Mark Baker:
PHP Generators – Sending “Gotchas”
Oct 11, 2016 @ 11:54:52

In this post to his site Mark Baker has shared some "sending gotchas" when generators are used in you PHP code. The focus of the article is on the "sending" part, pushing data into the generator for evaluation and use.

If you’re reading this, you’re probably already aware of just how useful PHP’s Generators are for improving performance and/or reducing memory overheads while keeping your code clean and easy to read.

Unlike their equivalent in some programming languages, PHP’s Generators allow you to send data into the Generator itself; not simply at initialisation (the arguments that we pass to the Generator when instantiating it); but also between iterations. This has its own uses, and again, allows us to move code from our main blocks and methods into the Generator itself. [...] However, there are a few “gotchas” when we combine Generators that both return and accept data in this way, and it really helps to be aware of them when we’re developing, otherwise it can create problems.

He starts simple, showing a generator that uses integers passed in as the starting number and addition interval for each loop. He gets a bit more complex in his next example, having a method called inside the loop. While the first instance of this behaves as expected, the second (after minor modification) yields unexpected results. He walks you through what's happening to produce those results and one possibility on how to get it corrected.

tagged: generator gotcha issue unexpected results debugging workaround

Link: https://markbakeruk.net/2016/10/08/php-generators-sending-gotchas/

Generating Code Coverage with PHPUnit and phpdbg
Sep 06, 2016 @ 12:36:23

In this post on his Medium page Elton Minetto shows how to generate the code coverage of your PHPunit tests with better performance using phpdbg.

In a previous post (in portuguese) I explained how to identify tests that are taking too long to execute. In this post, I’ll show you how to increase the performance of code coverage report generation using PHPUnit.

In the phpunit.xml file it’s possible to add configurations to generate reports related to the tests that are being executed. [...] In addition to changing the phpunit.xml file, to generate this information we also need to install the extension XDebug. However, by installing it we get a substantial decrease in performance.

He shows an example of the time difference in running the tests (about 1 minute without versus 22 with XDebug). He went looking for a better way and found this post talking about using phpdbg instead. He includes the "brew" commands to get everything you'll need installed and how to use phpdbg with your coverage calls rather than XDebug. However, as is pointed out at the end of the post, the results are slightly different but they're close enough to help you know what code to target next.

tagged: codecoverage phpunit phpdbg results performance tutorial

Link: https://medium.com/@eminetto/generating-code-coverage-with-phpunite-and-phpdbg-4d20347ffb45#.we2bst8uk

Laravel News:
How to Create A Most Popular List with Laravel and Google Analytics
Sep 02, 2016 @ 09:40:16

On the Laravel News site there's a new post showing you how to make use of the Google Analytics API in your Laravel application to find trending content in your site (most accessed pages).

Here on Laravel News, I wanted to generate a list of the most popular posts for the past seven days and display the results from most popular to least popular. To solve this problem I thought of two solutions. The first is to build my own tracking system so I could keep a count and then use it for ordering. However, that could generate a huge amount of data and it seemed like a solution that an analytics tracking service could handle.

As I was fumbling through the Google Analytics API I found a Laravel Analytics package by Spatie.be that allows you to easily retrieve data from your Google Analytics account and it seemed like the best way to solve this problem. Let’s look at how I used it to generate a list of popular posts here on Laravel News.

They then walk you through the installation (via Composer) and configuration of the library. This includes linking to more information about setting up the credentials for the connection. They then show how to use it to fetch the most popular pages and what the response looks like as a collection. Finally they show you how to create a wrapper class you can easily reuse anywhere in your application to fetch and display this "trending" information". The post ends showing you how to create a "View Composer" that only fires when the view is being rendered, not on every request.

tagged: laravel googleanalytics trending popular page results tutorial package

Link: https://laravel-news.com/2016/09/most-popular-list-laravel-google-analytics/

Chema Garrido:
Speed test PHP vs Lumen vs Laravel
Aug 18, 2016 @ 12:08:53

Chema Garrido has written up a post sharing some results of a performance test (speed) between Lumen and Laravel also comparing it against Kohana and straight PHP.

I am working in the new EmailValidator!, and after developing the EU VAT API, I feel confident to develop it on Laravel Framework. But before we start… let’s test the speed of the stack.

I used my local computer a 8 cores i7 2ghz 8GB ram 512SSD. Apache2, PHP 7.0.8. Tested this test with siege 5 times for each and retrieved the highest.

The first part of the post shows the results in a tabular format but following this is the more detailed version, complete with the siege command executed and the code used. The results are interesting but seem to mostly fall into the real of micro-optimization as there's really not that much difference between the results (though the "Longest transaction" on the plain PHP code is an oddity).

tagged: laravel lumen performance speed test results framework

Link: https://chema.ga/speed-test-php-vs-lumen-vs-laravel/

TNT Studio:
Easy way of sending scheduled tasks output to Slack
Jul 06, 2016 @ 11:20:12

On the TNT Studio site they've posted a tutorial showing you how to automate scheduled tasks and output to Slack, the popular online communication tool (think IRC for the web). They show how to use a simple webhook setup to relay the results of a task back to a given channel.

What many of us grow accustomed to is having cron job output emailed to us in order to see if everything went ok. Laravel's task scheduler also supports emailing output of the commands but if you are like millions of developers out there then you are probably using Slack and it's possible that it crossed your mind that it would be great if we could get output of the cron command sent to Slack. So let's do that.

They then walk you through the setup of the Slack notifier class to send the data to Slack via a Guzzle POSTed request. The next portion puts this code to work and creates the code to execute the command and return the results. The "after" event is then used to make the Slack request and output the results to the waiting channel.

tagged: output slack channel chat cronjob scheduled results output guzzle

Link: http://tnt.studio/blog/task-scheduling-output-to-slack

Jordi Boggiano:
Typo Squatting and Packagist
Jul 04, 2016 @ 09:38:45

In a new post to his site Jordi Boggiano, lead developer on Composer and Packagist.org, talks about typo-squatting and Packagist, a trend that has come up in other communities but - so far - not as much in the PHP ecosystem.

Earlier this month an article was published summarizing Nikolai Philipp Tschacher's thesis about typosquatting. In short typosquatting is a way to attack users of a package manager by registering a package with a name similar to a popular package, hoping that someone will accidentally typo the name and end up installing your version of it that contains malware.

The thesis mentions https://packagist.org as a good example as we use vendor namespaces. [...] Despite this mitigating fact, it is still technically possible to squat the vendor name, so I wanted to take a look at our repository data and see if I could spot any bad actors.

He wrote a script on the current contents of the Packagist site to see if he could find any packages that were trying to take advantage of typosquatting. He describes what the script does and the results: a low number of issues where it mostly seemed to be user error, not malicious behavior.

tagged: typosquatting packagist results composer

Link: https://seld.be/notes/typo-squatting-and-packagist

Analysis of Coding Conventions
Jun 09, 2016 @ 19:05:26

On the SquizLabs site they've shares the results of their coding conventions analysis of PHP projects using the PHP_CodeSniffer tool.

PHP_CodeSniffer, using a custom coding standard and report, was used to record various coding conventions across 193 PHP projects.

They've broken it down by the list of rules included in the default coding standards including:

  • Array end comma
  • Class defined in namespace
  • Function has doc comment
  • Adjacent assignments aligned
  • CamelCase method name
  • Line length
  • Spacing before object operator

Each item on the list has the current measurements represented as graphs and a historical view about its previous usage. You can also view per-project statistics for a wide range of PHP related projects.

tagged: squizlabs phpcodesniffer coding conventions report results

Link: http://squizlabs.github.io/PHP_CodeSniffer/analysis/index.html

Freek Van der Herten:
Getting package statistics from Packagist
May 23, 2016 @ 10:18:07

In a post to his site Freek Van der Herten shows you how to gather information from the Packagist website about the number of times that your packages have been downloaded.

At my work I’m currently creating a new dashboard. That’s a fancy term for an html page sprinkled with some Vue magic that will be displayed on tv screen at the wall of our office. I won’t say much about the dashboard itself on this post, but I’ll make sure to write something on that in the near future.

One of the things I want to display on our dashboard is how many times our packages get downloaded (yeah it’s a vanity project, sorry about that :-)). To make this real easy our intern Jolita and I cooked up a new package called packagist-api. It uses the packagist api to fetch data about published packages.

They include an example of the package in use, fetching the list of packages for the "spatie" vendor and getting the details by package name. The results include more information than just the download count as well (including current version, maintainers and the basic description). The post ends with an example of filtering out the downloads counts and putting them into a collection for later use.

tagged: package statistics packagist library results tutorial

Link: https://murze.be/2016/05/getting-package-statistics-packagist/

SitePoint PHP Blog:
Benchmarking: Can AppServer Beat Symfony’s Performance?
May 19, 2016 @ 10:45:51

The SitePoint PHP blog has posted a new article comparing AppServer and Symfony on a performance level and wonders if the AppServer platform can outperform the framework on some base level functionality.

After the release of the first part of our Appserver series, it was clear through the ensuing discussions on both SitePoint and Reddit that we had touched a nerve for a good number of PHP channel’s devoted readers. I also quickly realized this new (for PHP) technology had a good number of serious doubters. One of the most poignant responses in the discussions was something along the lines of,

Needless to say, those doubtful and critical comments sounded like a real challenge. I was also very interested in finding out where appserver would land, if it were to be benchmarked against another well known PHP framework. [...] I decided to use my favorite framework, Symfony, to make the comparison. This is because appserver, as a stock PHP application server, also offers a good bit of important application functionality similar to Symfony.

They start with the approach they took to the comparison and how they set up the systems to evaluate the difference between the two (including hardware specs). The remainder of the post shares the results of several Apache Bench runs - the raw command line output - and more graphical versions of the same information (bar graphs). While there are a few "wins" on the AppServer side, overall it came in a bit slower (mostly because of the technologies involved in every request, however).

tagged: appserver appserverio performance symfony comparison benchmark results

Link: https://www.sitepoint.com/benchmarking-can-appserver-beat-symfonys-performance/

Mark Baker:
Anonymous Class Factory – The Results are in
May 13, 2016 @ 12:15:17

Following up on his previous post about anonymous classes and a factory to generate them, Mark Baker has posted about the results of some additional research he's done on the topic and four options he's come up with.

A week or so ago, I published an article entitled “In Search of an Anonymous Class Factory” about my efforts at writing a “factory” for PHP7’s new Anonymous Classes (extending a named concrete base class, and assigning Traits to it dynamically); and about how I subsequently discovered the expensive memory demands of my original factory code, and then rewrote it using a different and (hopefully) more memory-efficient approach.

Since then, I’ve run some tests for memory usage and timings to assess just how inefficient my first attempt at the factory code was, and whether the new version of the factory really was better than the original.

His four options that finally worked somewhat as he'd wanted were:

  • A factory that returns an instance of a concrete class using the traits he wants
  • A factory that returns an anonymous class extending a concrete class that uses the traits
  • His original Anonymous Class factory and extending the result with the traits
  • His second version of the Anonymous Class factory that creates the instance, caches it and returns a clone

He also includes the code he used to run the tests of each factory method and shares some of the resulting benchmarks (with a few surprises).

tagged: anonymous class factory results options benchmark

Link: https://markbakeruk.net/2016/05/12/anonymous-class-factory-the-results-are-in/