Tag Archive for 'PHP'

PHP Team Development book review

Book coverSo I’ve finally finished the book!  OK, I finished it a couple weeks ago but haven’t had a chance to post up a review yet.  Of course, I had every intention of finishing it a lot earlier considering I was flying for nine hours to the States and then another few hours on to Mexico – and the journey back again! – but that really was just wishful considering I was travelling with my two year old son.  Oh well! :-)

On with the book review…

The book, as the title makes it plainly obvious, is about developing your team in relation to working with PHP.  It’s aimed at, well, pretty much anyone who has an interest in developing or working in a team, be it managers who need to set up and manage teams or developers working within a team who want to improve their work flow and procedures, or anyone in between.  It does this by giving an overview on several subjects, but doesn’t go as far as to tell you that you must do x, y or z.  This is understandable, though, as every team is different and the book acknowledges this.

Continue reading ‘PHP Team Development book review’

PHP Team Development (book)

Book coverThe other day I had a new book sent to me called PHP Team Development, written by Samisa Abeysinghe and published by Packt Publishing. Unfortunately, it arrived at work when I was on holiday so I haven’t been able to have a look at it yet. :-/ However, I’m back today and have the book in my hands (well, not literally, of course, else typing would be much more difficult), so am looking forward to diving in to it.

Hopefully have a bit of a review posted up here some time soon!

Shorten urls automatically with a Zend Framework filter

I think we can all agree that URL shortening services are great and are very handy to tidy up those long and obnoxious links. However, a lot of the time people simply forget to use them, or often don’t know about them in the first place. I’ve noticed this in a blog system I wrote using Zend Framework. On one hand I love that people post messages, but on the other it annoys me that they may supply a link that is so long it breaks the formatting of the page, or looks just plain ugly.

So what are my options? I could train everyone who posts blogs on the system to use a url shortening service or I could manually tweak all the links myself. As solutions they are not very practical at all; I don’t have the time to change any/all links myself, and I certainly don’t have enough patience to train everyone! So an automatic way of doing things is needed, and the filtering in Zend Framework comes to the rescue!

Continue reading ‘Shorten urls automatically with a Zend Framework filter’

Quick and easy email encoding view helper

Here’s a quick and easy view helper for Zend Framework that will encode an email address. It will encode just an email address or return a whole mailto link. The encoding is basically the same as in the Smarty template engine.

Obviously there’s a lot of room for improvement; javascript encoding, representation as an image, and so on… but then it wouldn’t be quick an easy – it’d be slightly longer and just a little more complex. ;-)

Continue reading ‘Quick and easy email encoding view helper’

Zend Framework 1.6RC1

If you haven’t heard already, Zend Framework 1.6RC1 is out and has lots of interesting new features. Finally there’s a SOAP component (seemed odd to me to have an enterprise-level framework without it!) There’s also a paginator, XML configs can have attributes, Dojo integration and lots more.

It’s been out for about 10 days as of the time I write this, but the only new thing I’ve tried as of yet is the Zend_Paginator component. And I must say that I am very happy with how easy it was to set up and integrate in to a site… Essentially, I just had to pass my select object to the paginator and write a view partial to handle how it looked – it was that easy! With the output of the pagination put in to a partial it makes the whole thing very easy to rebrand and configure to exactly how you want it to look. This is definitely a component I’m going to be using a lot.

Looking forward to using the other new features, too. There is a Zend Webinar to show the new features of ZF1.6 coming up on the 13th August, 2008. Also one in September to go over integrating the new Dojo features.

Application running really slow

While working on an application built on Zend Framework, I experienced a really odd slow-down of the system while running on the web cluster at work as opposed to my machine at home. I couldn’t see what the issue was myself, and it seemed to baffle people on #zftalk a bit as well as work colleagues. The speed difference was quite dramatic – going from near instant on my home computer to around 30 seconds for a page display while running on the cluster.

Naturally, this required a fair amount of investigation…

It was quickly ruled out to be any fault of ZF. After all, it is being used by companies such as IBM, Zend, Sourceforge, Fox, and more. If the framework were not suitable and produced slow results then they would obviously not use it, nor would any of you!

Next to be ruled out was custom code built on top of ZF. With the exact same code-base producing faster results on one machine and not on another it was highly unlikely to be the code.

Profiling the code proved a little helpful. I profiled the database connection for each query and ruled out any slowness with that as they were taking fractions of seconds. Code profiling was a little bit more tricky, as everything seemed proportionally slower, not any one thing in particular. However, the Zend_Loader component seemed to be taking quite some time to perform its tasks.

With a little command-line magic (using ktrace, kdump, grep, awk, etc. – not by me, but by talented colleague) it was determined that the OS itself, Mac OSX ‘Tiger’, was mainly to blame. The cause of the problem was trying to determine relative paths and the slow speed at which Tiger was doing this… As I understand it, to determine the current directory, ‘.’, the OS needs to back track all the way to the root, get the whole list of directories and work out which inode matches the one your current path is, and then work its way back down the directories until it finds a match. Once it’s done that you have your current path. If it sounds intensive, that’s because it is.

When comparing Tiger to Leopard we were seeing a 1000x improvement (4 microseconds as opposed to 4 milliseconds) to do various getdirentries() calls.

If you used the include path for a handful of files you’d never notice a significant drop in speed, but the application I’m working on, together with ZF will typically include 140+ files.

So how was the issue resolved?

For the short term there was a very simple fix; simply alter the include path so that the current path is last to be checked and the more significant paths (such as where the application or Zend library is located) are first. This simple tweak took a 30+ second load time to around two seconds – a vast improvement! Still, two seconds is not ideal so we will be having Leopard-based machine installed on the web cluster to see if that also helps to increase performance.

I’m curious; has anyone else had a similar problem?

StringToTitle filter

I like the filtering capabilities of the Zend Framework, but for some reason there doesn’t seem to be a string to title case filter (though there is a string to upper and string to lower). So here it is:

Continue reading ‘StringToTitle filter’

Auto generating basic models for a Zend Framework app

Do you have a database with foreign keys and just wish you could have something automatically create your ZF models from it? Well, today that was me. So as a little proof of concept, this is the code I came up with to do it for me…

But before we get to that, a few caveats:

  • It’s just a proof of concept
  • The output needs updating for proper reference names, etc.
  • Outputs everything to screen in one go and doesn’t save the files.

However, it might be handy to someone, so I post it up for your comments.
Continue reading ‘Auto generating basic models for a Zend Framework app’

Force a file download

Here’s a small function that will allow you to force a file download.

/**
 * Force a file download via HTTP.
 *
 * File is required to be on the same server and accessible via a path.
 * If the file cannot be found or some other error occurs then a
 * '204 No content' header is sent.
 *
 * @param string $path Path and file name
 * @param string $name Name of file when saved on user's computer,
 *                     null for basename from path
 * @param string $type Content type header info (e.g., 'application/vnd.ms-excel')
 * @return void
 * @access public
 */
/* public static */ function download($path, $name = null, $type = 'binary/octet-stream')
{
    if (headers_sent()) {
        echo 'File download failure: HTTP headers have already been sent and cannot be changed.';
        exit;
    }

    $path = realpath($path);
    if ($path === false || !is_file($path) || !is_readable($path)) {
        header('HTTP/1.0 204 No Content');
        exit;
    }

    $name = (empty($name)) ? basename($path) : $name;
    $size = filesize($path);

    header('Expires: Mon, 20 May 1974 23:58:00 GMT');
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Cache-Control: post-check=0, pre-check=0', false);
    header('Cache-Control: private');
    header('Pragma: no-cache');
    header("Content-Transfer-Encoding: binary");
    header("Content-type: {$type}");
    header("Content-length: {$size}");
    header("Content-disposition: attachment; filename=\"{$name}\"");
    readfile($path);
    exit;
}

Very easy to use, too! Here are some examples of how you might call the function:

download('./myfile.txt');

download(__FILE__, 'a file for you.php');

download('/home/you/files/spreadsheet.xml', 'ssheet_' . date('Ymd'), 'application/vnd.ms-excel');

Simple image view helper for Zend Framework

Here’s a simply view helper for the Zend Framework that can be used to display image tags. It checks to see if the image file exists and if not then it’ll use the data url scheme to output a very simple image that, ironically, says ‘NO IMG’ on it. :-) Please note, though, that I’ve only seen Firefox support this scheme, as wonderful as it is!

Continue reading ‘Simple image view helper for Zend Framework’