PHP

The Rise and Fall of PHPclasses

I started playing around with PHP somewhere between 1999 and 2000 which does not necessarily make me a good PHP developer, but I have seen a lot of PHP History. One of them is PHPclasses, which existed way before GitHub, Packagist and all the Fanboying in the PHP community.

It was my go to destination for PHP libraries as I had a early dislike for PEAR (for no real reason actually) and there was no real alternative (except maybe SourceForge??). I even “contributed” a shitty class (SOCKS5 Client) in 2008 (did a modernization on GitHub), but even though PHPclasses was in my bookmarks (and I occasionally stumbled on it via Google) it was always very annoying to use:

  • forced registration to view source code
  • some great talented developers, but also a lot of low quality “libraries” (usually mixed up with hardcoded HTML)
  • nothing against advertisement, but the placement combined with the “layout” did not make it fun to browse
  • shitty layout was shitty back then (even then it felt like from the 80s), the recent relaunch still feels like from the 80s, but with more graphics
  • no repository
  • mainly something like “one author = one library” as opposed to “collaboration” which you will often find on GitHub

Today Phil Sturgeon, whom I had the pleasure to meet at the PHP mini conference in Cape Town this year (no idea who he was before that, but funny outgoing guy, and seems to know more about PHP programming than I do) started a wave that many of us developers were not saying out loud:

PHPclasses.org sucks!

 

Do me a favour. Tweet this way with your opinion; A) You use and love @PHPclasses. B) You wish it would fuck the fuck off. C) Other.
— Phil Sturgeon (@philsturgeon) August 4, 2014

The response on Twitter is huge, and you can most probably guess which answer was voted most.

Funny enough Manuel Lemos decided to chip in, turns out he sucks* as well. Very stubborn and failing to acknowledge feedback, he still insists that forcing user registration for viewing source code is the right thing to do.
Sites like GitHub actually give a perfect example that you can perfectly have guests view source code and only force registration for “ratings” or “subscribtions”. And there is not less “fame” for the developers.

@philsturgeon That is your opinion and it is OK for you to disagree with the other near 1.3 million registered users of @phpclasses .
— Manuel Lemos (@manuellemos) August 4, 2014

Anyhow, the same reason why PHP: The Right Way should be preferred over old sites like tizag or tutorialspoint, if you are new to PHP, please do not go to PHPclasses.org but use Packagist (you do not even need to use composer) or GitHub to find useful PHP libraries that will help you accelerate development on your next project.

I do hope PHPclasses.org will get a grip and do a massive relaunch, but I doubt someone as stubborn as Manuel can pull it off. The decline of his Alexa Ranking should indicate that “the-PHPclasses-Way” will not last forever.

*Update: Matthias N. noted in the comments that it sounded mean. I just wanted to clarify: this should not be meant as a personal attack on ML, as I do not know him personally and therefore would have no reason to attack him personally. So saying “he sucks” is my simplified opinion about him based on how he has been reacting to criticism for a long time. It is a shame since his website had quite some large potential & reach.
Additionally, while I am very “harsh” in my post, I am also not shy to wish him the best with the project and hopefully a “turn” in his current path.

Cleaning up PHP short-open-tags with Eclipse

One major problem of old/legacy or unmaintained PHP scripts is, that they usually use short-open-tags to enclose PHP code:

<? echo “foobar”; ?>

This used to be common in the old days, but trying to run those scripts on a modern LAMP stack will most likely result in 500-code errors.

Sure you could just enable short_open_tag in the php.ini and it would solve the problem. But even though there are no official hints that it might get deprecated or removed, it is still a bad practice. The Basic Coding Standard by the PHP Framework Interop Group clearly states:

Files MUST use only <?php and <?= tags.

So why should you hold on to bad practices, if you can just easily mass replace them to normal open-tags? Eclipse offers a feature where you can do a project based search & replace, and even use Regular Expression with group matching. Follow the steps below to refactor your (or someones else) code.

Steps to fix under Eclipse (PDT):

1) Select via the “File Navigator” your root project folder

Eclipse File Navigator

2) Go to the menu: “Search” -> “File…”

3) Configure as seen in the screenshot below, type into “Containing text:” the following: <?($|s+)

Eclipse Search & Replace in Files

4) Click on the button “Replace…”

5) Again, configure as seen in the screenshot. Type into “With:” the following: <?php$1

Eclipse Regular Expresion Text Matches

6) Click on “OK” -> Done! 🙂

Basically with that regular expression we just used, you are searching for all “<?” occurrences that are followed by either a newline or white space (tabs, spaces). It will then replace it by “<?php” including the same amount of white spaces (or newline) after. This will ensure we are not breaking the code or changing anything else except the PHP short-open tag.

Killing MySQL Slow Queries with Xcache

I currently manage a high traffic Image Hoster with 10 million Page-Impressions per day causing high load on the Web Frontend Server and the DB Backend Server for some months now. My budget did not allow me to scale horizontally so I had to optimize the web application by killing a slow MySQL query with the usage of Xcache. Due to the website’s structure I was not able to use the Smarty Caching function as this would easily generate 2 million files and cause high disk i/o.

Pictures are just more than words.. so have a look at the screenshot of the MySQL-Server’s load before and after the optimizations (which went online on 6th October) – its like day and night 😉

Our Web Server uses Lighttpd 1.5 / SVN + PHP-FPM 5.3.3 (guys.. spawn-fgci is deprecated 😉 ) + Xcache (PHP Accelerator and varcache) to deliver static files and dynamic pages which connect to a separate MySQL 5.0 Server.

Unfortunately with a load of 5-10 (8 CPU cores) and 60-100% CPU usage on each core (!!) our MySQL Server was pretty much overloaded 😉 .

A big downside when having bottlenecks in your PHP-Script – usually caused when relying on external resources (like file_get_contents, cURL, massive non-asynchronous DNS-Lookups, MySQL queries, etc.) – is obviously the much higher execution time. This results in having a lot of PHP (or even worse Apache) processes being spawned or in use. You will easily get a over filled backlog or in worse case your Web server will start swapping – either way your website will slow down dramatically and you will lose a lot of visitors.

At first I checked the php-fpm.log.slow for scripts with too long execution times, just to make sure that this was not a PHP problem. There were a lot of scripts hanging during mysql_query() – so it was pretty clear where to look next.

Next I took a look at the MySQL Slow Query log and summarized queries which appeared most of the time. I was able to filter out the following query (simplified):

1
SELECT DISTINCT col1, col2, FROM table WHERE col3 = col4 AND id IN (SELECT id FROM table2 WHERE x = $variable) AND (SELECT id FROM table3 WHERE a = $variable) OR col5 = 1

A query with two sub-SELECTs and DISTICNT did not sound fast to me – especially not in that frequency it was requested – which was the key factor, as querying it on an empty MySQL-Server did not cause any problems .

Before putting the query into Xcache I checked all conditions and figured out that “… OR col5 = 1″ was never true, as currently no data had that value. I decided that if some feature / function based on that condition was not used since years, it will not be needed in future anyway, so I removed it.

Now I was finally ready for Xcache. This is only a very simple example how to cache individual SQL-Queries like

1
SELECT * FROM table WHERE name = '$variable'

in your PHP-Script:

1
2
3
4
5
6
7
8
9
if(xcache_isset("prefix_" . md5($variable)))
{
    $result = xcache_get("prefix_" . md5($variable));
}
else
{
    $result = mysql_query("SELECT * FROM table WHERE name = '$variable'");
    xcache_set("prefix_" . md5($variable), $result, (60 * 60 * 6));
}

So from now on, every SQL-Query will only be done once every 6 hours. Remember: we don’t want to fill up our Xcache for no reason.. so try only to SELECT columns which we really need. The biggest advantage with Xcache in contrary to other caching systems (e.g. Smarty Cache) – it has a garbage collector! So you don’t need to worry about zombie cache entries. Just try not to go out of memory, e.g. assign enough memory for your needs in the php.ini under the xcache section.

And set a reasonable time-to-live, not too short so enough data gets cached and load goes down, but not too long which could cause a too high memory usage.

Thats all 🙂

I was able to lower the load and CPU usage of our MySQL server by approx. 850%! How about you, were you able to optimize your website? Show off your awesomeness! I did by committing with following comment into svn 🙂