Try-catch suppress?

PHP never ceases to amaze me. Just the other day a colleague discovered that you can suppress error messages reported by file_get_contents() using the try-catch statement. That should’ve raised an eyebrow. In case you missed it here’s the sample code run from php’s interactive console:

Sample code, nothing odd (yet)


qbook:mobilesns quinton$ php -a
Interactive mode enabled

<?php

echo file_get_contents('bleh');

Warning: file_get_contents(bleh): failed to open stream: No such file or directory in /Library/WebServer/workspaces/default/mobilesns/- on line 3

Call Stack:
19.3675 673612 1. {main}() /Library/WebServer/workspaces/default/mobilesns/-:0
19.3914 673676 2. file_get_contents('bleh') /Library/WebServer/workspaces/default/mobilesns/-:3

Dump $_SERVER
Dump $_GET
Dump $_POST

Predictably, the code snippet above confirms that php will report an error if you attempt to open a file that does not exist. You may have noticed, my php installation has some extra debug output because I’m using the XDebug extension. I recommend it. Highly.

This is where it gets weird

Again, from the console. Here we place the same call to read a non-existent file within a try-catch statement.


qbook:mobilesns quinton$ php -a
Interactive mode enabled

<?php

try
{

echo file_get_contents('bleh');

}
catch (Exception $e)
{

echo "Exception is caught";

}

This time around the error is not reported by php nor is the “catch” executed proving that the error message is suppressed. Is it a bug or a side-effect? Anyone have any thoughts on this? “Its not a bug its a feature!” ~ comes to mind :-)

My php version, in case you’re wondering


qbook:mobilesns quinton$ php -v
PHP 5.2.5 (cli) (built: May 27 2008 11:45:48)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
with Xdebug v2.0.4, Copyright (c) 2002-2008, by Derick Rethans
with Zend Debugger v5.2.14, Copyright (c) 1999-2008, by Zend Technologies

Published in: on March 19, 2009 at 10:17 pm Comments (15)
Tags: ,

session-clustering with memcache

You need php session-clustering and you need it done yesterday. The project is at risk. The suits are breathing down your neck and monitoring your every tweet. Memcache to the rescue :-)

Installing memcached

Not installed? Get it here. Alternatively, install via a *nix package manager eg. apt, yum, rpm etc. Start your memcache servers:


/usr/local/bin/memcached -u root -d -m 1024 -l 0.0.0.0 -p 11211

Add one more to the pool: (this will make sense later. promise)


/usr/local/bin/memcached -u root -d -m 1024 -l 0.0.0.0 -p 11212

Note: the port number differs for the second memcached daemon.

Installing memcache extension with “session feature”

PHP “talks” to a memcache server via the memcache extension but you already knew that. Something less known is that when installing this extension you can enable/disable the extension’s session.save_handler feature.


qbook:Locate quinton$ sudo pecl install memcache
Password:
downloading memcache-2.2.5.tgz ...
Starting to download memcache-2.2.5.tgz (35,981 bytes)
....done: 35,981 bytes
11 source files, building
running: phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
1. Enable memcache session handler support? : yes

Choose “yes”. You can now configure php to use memcache as its session store.

Configuring the memcache session store

ext/memcache introduces new directives to the php.ini. In this post I’ll use ini_set() within a php script so you can just copy-and-paste to try it out yourself. In your production environment you’ll prolly want these directives inside your php.ini and/or apache vhost. You decide.



<?php

// firstly, override the default session save_handler like this
ini_set('session.save_handler', "memcache"); // PHP_INI_ALL Supported since memcache 2.1.2

// now tell php where to store the sessions. two memcache servers specified here. more bout that later
ini_set('session.save_path', "tcp://localhost:11211, tcp://localhost:11212"); // PHP_INI_ALL Supported since memcache 2.1.2

// getting interesting. if primary server is down talk to the others in the server pool
ini_set('memcache.allow_failover', "1"); // PHP_INI_ALL Available since memcache 2.0.2.

// more tweaking stuff follows here

// may want to adjust this
ini_set('memcache.max_failover_attempts', "20"); // PHP_INI_ALL Available since memcache 2.1.0.

ini_set('memcache.default_port', "11211"); // PHP_INI_ALL Available since memcache 2.0.2.

ini_set('memcache.chunk_size', "8192"); // PHP_INI_ALL Available since memcache 2.0.2.

ini_set('memcache.hash_strategy', "standard"); // PHP_INI_ALL Available since memcache 2.2.0.

ini_set('memcache.hash_function', "crc32"); // PHP_INI_ALL Available since memcache 2.2.0.

?>

A crude test

This code can be appended to the php snippet from above


<?php

session_start();

echo "Session save_handler is: ".ini_get("session.save_handler")."
";
echo "Session save_path is: ".ini_get("session.save_path")."
";

if(isset($_SESSION['bleh']))
{
echo "Bleh is already set: ".$_SESSION['bleh']."
";
}
else
{
$_SESSION['bleh'] = 'bwahahha';
echo 'Bleh has been set with: '.$_SESSION['bleh']."
";
}

?>

Where’s the clustering you ask

Well, truth be told there isn’t any. What we have so far is a memcache pool consisting of 2 servers. PHP is configured to write to the pool. PHP reads/writes to the server pool in order specified by “session.save_path” ini directive. For reads PHP will request a cache object by key from the pool. Because “failover” is enabled PHP will query the pool of memcache servers one-by-one until the request is fulfilled. Every silver lining has a cloud.

Fragmentation

In the event of a memcache server crash, fragmentation will occur. Therefore the pool will have an identical cache and its exceptionally hard to tell which keys each memcache server is storing. When a memcache daemon crashes it loses all data. Memory, by nature, is volatile. You have been warned.

Replication

This method is most commonly refered to as client-side replication BUT is better described as redundancy. There is a method of achieving server-side replication (think mysql replication). AFAIK repcached is the only available patch of its kind. I would like to try it but haven’t had the opportunity. Its a long story.

Summary

Memcache-based session save_handler is a quick-win but its NOT scalable. As your server farm grows the cost of maintaining and managing this solution is complexity. Also, consider the implications of managing a php.ini per web server and the repercussions of the failover feature. Nasty. However, there is good news. A libmemcached-based extension is on the way.

Getting real

If you have more time to spare take the safer option ie. storing sessions in a database. You could write a custom session save_handler or just use Zend framework’s Zend_Session_SaveHandler_DbTable. What could be easier?

Published in: on March 2, 2009 at 11:26 pm Comments (26)
Tags: , , ,

Test-drive: MySQL Workbench

Workbench?


MySQL Workbench is a cross-platform, visual database design tool developed by MySQL. It is the highly anticipated successor application of the DBDesigner4 project. MySQL Workbench will be available as a native GUI tool on Window, Linux and OS X.

Some history

MySQL Workbench has been in the making for a while. Building on from DBDesigner 4 I’m sure there’s a community of anxious MySQL developers who can’t wait for a free and quality ERD-modelling (and more) tool for MySQL. The mysql command-line client is brilliant. I love it and will always use it BUT there’s no substitute for managing entity relationships visually.

I’ve been waiting on MySQL Workbench for ages (more specifically a Mac OS X release). On the day the windows version was released I soon installed it in my Windows XP virtual machine. It works really well but realistically its too painful to use via VirtualBox. Could be a breeze with VMWare Fusion. Anyone tried this?

It really is Alpha

MySQL Workbench 5.1.7 is an alpha release for Mac OS X and Linux which prolly explains why the dmg is only 7 megabytes. Features are missing!

And lift-off

Installation was easy. Typical drag-and-drop to Applications folder installation. First launch was all over in about 10 seconds. Workbench crashed when I tried File -> Open Recent menu item. Strange. Evidence follows:

Workbench Crash

Workbench Crash

And lift-off

On second launch I avoided “Open Recent” menu item. I tried the reverse engineer from .SQL script feature. Workbench smartly recreated a schema from an old project schema.sql I had lying around. I couldn’t edit any of the tables. Workbench complained about a “missing editor”. Maybe it wants to integrate with MySQL Query Browser or something?

Workbench reverse engineer script example

Workbench reverse engineer script example

ERD’ing

Getting around an apparently buggy UI I did get to link some tables with many-to-one relationships. The experience was positively engaging. Workbench is already a vast improvement on the kludgy and buggy DBDesigner. Some screenshots to follow:

Workbench many-to-one example

Workbench many-to-one example

And just to verify that the “Open Recent” crash wasn’t an anomaly I tried it again. Kaboom. A rather strange bug indeed.

Conclusion

If you’re going to use Workbench on real projects I advise practicing the “save every 5 seconds” rule. It is but only an Alpha but a very promising one at that. Regardless. I’m excited. I can’t wait for the first stable release. To the MySQL Workbench (and entire team at Sun MySQL) thanks for great tools and a great database. Love it. Bugs and all.

Published in: on February 21, 2009 at 6:52 pm Comments (2)
Tags: , , , ,

Using MySQL 5.1? You’ve been warned

The other day I got really excited about MySQL 5.1. Finally, I can use prepared statements and the query_cache at the same time! And then I stumbled upon Monty’s blog post.

In case you missed it. A stern warning from Michael Widenius (Founder and original developer of MySQL).

That’s alot of bugs. Its not the volume of bugs thats alarming but the seriousness attached to them. Many are “crashing” bugs! Depending on what you use MySQL for they may or may not matter. Depending on how much data you have that bug list could leave you feeling rather uncomfortable about MySQL 5.1.

Three months has gone by since MySQL 5.1 was announced “Generally Available”. According to mysql performance blog there are some exceptional MySQL users out there. Some on the bleeding edge using the 6.0 releases already.

Is this all a rush to become “Enterprise Ready”? Is it Sun’s fault? Has MySQL lowered its quality standards?

Who knows. I’m keeping my options open (postgres?). Some ex-MySQLers are busy elsewhere. Hrmm. Maybe the relational database is doomed after all.

Published in: on February 17, 2009 at 1:00 pm Leave a Comment

Making an iso in mac os x leopard

The other day I desperately wanted/needed to install Ubuntu 8.04 Server as a virtual machine using VirtualBox. I could either install Ubuntu directly from the installation disc or install from a mounted ISO. I figured an ISO would be faster and more reliable. I looked all over Mac’s applications folder for a GUI utility but the for the life of me couldn’t find one to create an iso. I really thought Disk Utility would have that feature, it doesn’t right?

In *nix when the GUI fails you can always go old skool.

Open Terminal


qbook:~ quinton$ drutil status
Vendor Product Rev
MATSHITA CD-RW CW-8221 GA0K

Type: CD-ROM Name: /dev/disk3
Sessions: 1 Tracks: 1
Overwritable: 00:00:00 blocks: 0 / 0.00MB / 0.00MiB
Space Free: 00:00:00 blocks: 0 / 0.00MB / 0.00MiB
Space Used: 59:42:17 blocks: 268667 / 550.23MB / 524.74MiB
Writability:

Assuming you’ve already inserted the disc for copying. This command tells you where its been mounted.

Unmount the disk to be iso’d

qbook:~ quinton$ diskutil unmountDisk /dev/disk3
Unmount of all volumes on disk3 was successful

Using the location derived from the previous command. In my case the CD-ROM is /dev/disk3. Yours may differ

Make the iso


qbook:~ quinton$ dd if=/dev/disk3s0 of=ubuntu_8.04_lts_server.iso bs=2048
268667+0 records in
268667+0 records out
550230016 bytes transferred in 308.685634 secs (1782493 bytes/sec)

The real magic happens here. If you’re curious about progress you can watch the size of the newly created iso file “grow” in size using Finder or another terminal tab/window. Shouldn’t take longer than 10mins or so (for a 700mb cd)

Mount the ISO


qbook:~ quinton$ hdid ubuntu_8.04_lts_server.iso
/dev/disk1 /Volumes/Ubuntu-Server 8.

And to confirm the ISO is working. This command will mount the ISO as a readable volume. Should be viewable in Finder alongside all your other mounted devices.

Published in: on February 15, 2009 at 4:09 pm Comments (1)
Tags: ,

VirtualBox’ing Ubuntu Linux

If you’re expecting a long hack about how to get it working sorry you won’t find it here. Ubuntu just works in VirtualBox except for one not so obvious error message on first boot.

I couldn’t get a screenshot for some reason but the error message you will get on first boot of any ubuntu distro (desktop or server) in VirtualBox is:


Starting up ...
This kernel requires the following features not present on the CPU:
0:6
Unable o boot - please use a kernel appropriate for your CPU.

Huh? Yeah me too. I’ve encountered this issue before I just forgot the fix so I had to google it (again). The fix is:

  1. Power-off or shutdown the running virtual machine instance
  2. Select the “Settings” menu option from the context-menu on your VM instance (mine is called hardy lts in the screenshot)
  3. Then from the General tab pane select “Advanced” tab pane
  4. Enable PAE/NX

VirtualBox fixer upper

That’s it! You now have a useable ubuntu virtual machine running in OS X. yay. This is listed as a “Known Issue” on the ubuntu wiki

Disclaimer: Steps may vary on your guest OS. eg. it might require 50+ clicks and a cold boot when using winblows :P

Published in: on February 9, 2009 at 10:24 pm Comments (2)
Tags: , ,

php4 and php5 certification

At the beginning of 2009 I found myself a bit idle. A new year in a new company always starts out a bit slow. I decided that the best use of this abundant time would be to get PHP5 certified.

Not the first certification

Back in early 2005 the Zend certification first became available for PHP4. At that time I had roughly 2 years php experience. No South Africans were certified at that time. Barely 100 certified engineers worldwide. What a great achievement if I could join the elite few.

PHP4 certification process

I signed into Zend.com and ordered the certification voucher, exam study guide and practice exam book. Zend were very accomodating and offered a generous discount on pricing.

The php4 certification guide provided adequate preparation for the exam. Along with that I gained a deeper understanding learning many things along the way. And I’m grateful for that.

The practice book is excellent. One could simulate at least 5 exams from the pool of questions in the book. It also provides the explanations to the answers which again was a real plus. Once I was ready I booked the exam with Pearson Vue at a nearby testing centre.

From purchasing the certification goods all the way to writing the exam it was a smooth experience and most worthwhile.

Fast forward to 2009

PHP5 certification process was a little different. Not nearly as smooth. This time around I ordered the pdf version of the certification study guide and instead of a practice exam book Zend now offers mock tests thru Vulcan.

Shortcomings in the study guide

PHP5 study guide lacks the depth offered by the php4 study guide. Perhaps its because PHP5 is a “bigger” language with additions such as MySQLi, PDO, Tidy, SOAP and all new XML extensions. Not forgetting the complete rework of the OOP model. Did I miss anything?

Vulcan needs work

To phparch and Zend’s credit they have devised a smart online mock testing platform but it needs refinement.

  • I had a vulcan token which was valid for 10 mock tests but a few tests later the token become invalid.
  • Questions were very poorly phrased. I couldn’t tell if they were trick questions or just a result of poor english.
  • Vulcan didn’t offer explanations for the questions that you got wrong. It didn’t even tell you which questions you got wrong. Boo

Exam time

Exam format is 70 multiple choice questions in 90 minutes. Some code samples to interpret, some theory questions, some function names to type in but nothing too hardcore. The real exam also had some oddly phrased questions which often required a double-take. No sweat. I finished 40 minutes early and got my test results immediately. Pass. Yay!

In the end I missed my goal of becoming the first South African to be zend certified in 2005 and I also missed becoming the first php5 certified engineer. However, at time of writing I am the only South African to hold both php4 and php5 certifications. w00t!

Thats an achievement but that isn’t my motivation for getting certified. I revel in proving to myself that I’m a competent and capable PHPer. Things grow, change and evolve so quickly one must always make room for learning.

Published in: on at 1:51 pm Leave a Comment
Tags: , , , ,

MySQL 5.1 fixes prepared statements

Its been a long wait. Yesterday I discovered that MySQL 5.1 is generally available. And the good news is prepared statements in mysql 5.1 can utilize the query cache :-)

Previously, the most elegant work around when issuing prepared statements from PHP was to emulate prepared statements client-side using PDO. The other obvious alternative is to not use prepared statements!

If you’re not using prepared statements maybe you should take a look. It’ll save you. Promise. MySQL 5.1 is generally available but I’m not so sure the package maintainers have got a chance to include it into the major distros as yet.

If you’re still on MySQL 5.0 or less… beware! Prepared statements are not stored in the query cache :-(

Published in: on February 6, 2009 at 10:41 am Comments (1)
Tags: , ,

Smarty. Solving the wrong problem since 2001

Today’s rant is about the Smarty templating system for PHP. A recent blog post by Paul M. Jones has rekindled my strong feelings against Smarty.

Honestly its nothing personal. Its just I can’t believe developers are still using Smarty or that they ever started using it. Unthinkable.

Some background

About 4-5 years ago I stumbled upon Harry Fuecks’ phppatterns.com (site is a whole lot different now). It opened up a whole new world for me. I discovered N-tier architectures and Design Patterns (save that for another day). The most significant revelation was the Model-View-Controller design pattern. Finally, I found a proven industry standard for separating Business Logic from Presentation Logic. My life was complete. Hyperbole.

Next steps

I needed a templating system to compliment my new plans for using MVC. I researched the dozens of templating systems out there. Back then I recall 4-5 credible options existed. Of course, smarty was one of the options. But as I read the docs and looked over the examples I soon realized Smarty solves the wrong problem. Instead, I chose phpsavant

Smarty solves the wrong problem

I should explain that statement. Its only right to support a statement like that but I won’t because Paul M. Jones does a better job of explaining it. And I agree with every word.

Smarty book author repents

Hasin Hayder once wrote a book entitled “Smarty PHP Template Programming and Applications”. Soon thereafter he realized “there is no need to use an external template engine like smarty”.

In closing… just Say No to Smarty!

Published in: on February 5, 2009 at 6:46 pm Comments (5)
Tags: , ,

vim tips for php programmers

This post assumes you’re a php developer and you’re at least vaguely familiar with the VIM text-editor. For those who aren’t here’s a quick run-down:

VIM is a powerful command-line text editor usually a default package in the majority of *nix distros (that includes Mac’s OS X). I believe there are ports available for other OSes, even winblows.

Firstly, vim is a man’s editor. Whiners and GUI lovers can stop reading now. Although, VIM offer GUI ports such as MacVim. Men with thick beards, please stay tuned.

The lightweight text-editor VS bloated IDE debate

It’s a matter of preference really. Some like to use IDEs such as Zend Studio for Eclipse, Eclipse PDT, phpeclipse, Komodo ActiveState, Dreamweaver (*oh dear*) etc. And some prefer lightweight editors such as Coda, Textmate, Notepad++, Textpad, GNU Nano, Emacs, Vi, VIM etc.

I use both Eclipse PDT (just installed version 2.0) and VIM 7.x. Lightweight editors and IDEs are powerful in their own right. I’m not going to argue that you should use one over the other. Use whatever works for you. This is not an evangelism post.

Some say the following about VIM

“vim is for people who like pain – I’ll know what to order at your bachelor’s party” – @donaldza

“#vim is great for scripts, configs etc but let’s be honest, this is the 21st century ;) I still use it for merging/diff” – @donaldza

Okay, maybe just @donaldza has an issue :-) the rest of us continuing reading

What VIM offers programmers (sysadmins too) in general

  • powerful linguistic interface (ie. command-driven so your fingers never need to leave the keyboard)
  • syntax highlighting
  • so lightweight you can edit files over SSH connections on a distant remote server
  • available on just about any *nix
  • blah blah yada yada

Straight into the good stuff: Tips for PHP programmers

Assuming you’re already familiar with the basic editing features of such as copying, pasting, yanking, deleting and so forth. If not, try “vimtutor” from your *nix command-line.

This is a copy-paste of my .vimrc file. You can find yours in $HOME/.vimrc. If VIM is already open you can use the “:edit $MYVIMRC” command. I wouldn’t mess with the system-wide default (usually /etc/vimrc/vimrc). Other vim users on your server might not appreciate your preferences

Syntax highlighting. You gotta switch it on like this:


syntax on

Usually triggered by the file extensions you’re using. This list of extensions is configurable so you can highlight your .inc’s too. That’s an old habit you should toss btw. You might wanna change your terminal colour theme. White background works best with syntax highlighting. Then again you could also change VIMs colour theme.

Search highlighting.

set hlsearch

Includes simple and regex searches within VIM

Tabs as spaces.

Often you want X spaces instead of a real tab

set tabstop=4

Starting to get cool…

set autoindent

Autoindent remembers the indentation of the previous line and so your next line starts directly beneath. Python okes should appreciate this one

In the middle of a series of keystrokes?

set showcmd

Often vim has double-barreled commands that require a sequence of more than one keystroke. Showcmd displays on the footer of your screen informing you which command you have initiated

Search and show matches as you type

set incsearch

In the middle of issuing your full search phrase incsearch will highlight matched words. Useful when constructing a long-ish regex

PHP-specific highlighting

The general syntax highlighting offers some php syntax but not everything

" highlights interpolated variables in sql strings and does sql-syntax highlighting. yay
autocmd FileType php let php_sql_query=1
" does exactly that. highlights html inside of php strings
autocmd FileType php let php_htmlInStrings=1
" discourages use oh short tags. c'mon its deprecated remember
autocmd FileType php let php_noShortTags=1
" automagically folds functions & methods. this is getting IDE-like isn't it?
autocmd FileType php let php_folding=1

Note: This is just the beginning of code-folding. A number keystroke combinations exist to expand, contract foldings within the opened file

Syntax checking within VIM (sort of)

" set "make" command when editing php files
set makeprg=php\ -l\ %
set errorformat=%m\ in\ %f\ on\ line\ %l

To use, simply issue “:make %” command inside of VIM to check the syntax of your php against the interpreter. Syntax highlighting can only do so much

Highlighting matching brackets/parentheses

" set auto-highlighting of matching brackets for php only
autocmd FileType php DoMatchParen
autocmd FileType php hi MatchParen ctermbg=blue guibg=lightblue

This automatically highlights brackets and parentheses as the cursor passes over them. Colours configurable

Auto-completion of functions and constants

" autocomplete funcs and identifiers for languages
autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete

The above is really auto-completion for more than just PHP as you can tell. Auto-complete is triggered in the middle of typing the identifier

” Basically, while in insert mode, you can type <C-x> <C-o> to have vim attempt to autocomplete the current keyword. If more than one possibility exists, it shows a dropdown, and you can use your arrow keys to highlight the keyword that you wish to use.

But it gets better! Not only does it do this kind of autocompletion, but it also opens a small ’scratch preview’ pane showing the function/method signature — i.e., the expected arguments and return value! ” – Matthew Weier O’ Phinney

Auto change to to editing file’s cwd


" auto switch to folder where editing file


autocmd BufEnter * cd %:p:h

Often one would open a file for editing in VIM from a different working directory eg. vim ~/somewhere_else/myfile.php. This command automagically changes vim’s current working directory to that of the file you’ve opened making it easy to open other files which are close by on the filesystem

Split-panes

Often you want to edit more than one file inside of VIM without having to open and close between them. Using “:split otherfile.php” and “:vsplit otherfile2.php” you can create horizontal and vertical split-panes respectively. You can of course split the same file across two-panes simultaneously using “:split %”

You can split splitted panes to infinity but…

Tabbed-panes are better

With the “:tabnew filename.php” or just “:tabnew” commands you have tabbed panes. voila! “:tabclose”, “:tabprev”, “:tabnext” does exactly what one would expect

You can also use keystrokes such as “gt” to jump between tabs quickly. Of course there are dozens more in-editor commands and keystrokes associated with the above tips/preferences. This post was merely meant to open your eyes to a whole new world of powerful lightweight text-editing. You can find a whole lot more in the resource links that follow and vim help! (”:help”)

More cool vim stuff

Published in: on at 6:40 am Comments (16)
Tags: ,