Archive for the 'PHP' Category

The biggest bottleneck: developer time, not double quotes

Saturday, 2nd December, 2006

It’s hard to drag a community the size of PHP’s towards such things as standards and using frameworks. From my experience many PHP developers still struggle/refuse to adopt others standards.

When I started PHP, PEAR didn’t exist, and even when it did (and probably even today) much of community is used to “rolling their own” solutions for 99% of tasks. Most developers I’ve worked with don’t trust/use PEAR classes and I can’t really blame them. Most early efforts were authored by developers who at the time didn’t really grasp the OO concept and made god classes.

The Ruby community has one up here, their community was kick started by Rails – many good programming practices have been spoon fed from the start. Couple this with Ruby probably not being many Ruby-newbies first language. I digress…

In every company I’ve worked at code re-use has been low because “every problem is different”. Now this isn’t normally true, the nucleus of the project is but the rest is made up of much of the same stuff tackled on every project (database abstraction, session management, form validation, data sanitisation, the list goes on…).

What holds the PHP community back from adopting frameworks and standards is the majority of the community’s obsession of prematurely optimising the mundane (++$i vs $i++ in loops etc). Just check the comments in the manual on language basics, or most forums – how long has the single vs double quote debate been running?

I’ve read threads about removing white space and pre-processing include/require statements to speed up execution. Neither of these would address the true bottleneck in an application.

Only in circumstances where the coding has been exceptionally poor has PHP ever been a bottleneck on the sites I’ve worked on, 99% of the time it’s the database that you’ll run up against before seeing any PHP performance issues. This obession with squeezing performance before a performance issue has been identified scares most developers off the idea of using a “bloaty” framework that’s too heavy weight for their needs and instead they go back to re-inventing the wheel again.

When the larger community becomes seasoned enough and recognises the biggest bottleneck is developer time and not double quotes then hopefully we’ll see some proper standards materialise.

PHP5 Certification Announced

Thursday, 14th September, 2006

Somewhat late (just like me posting this news) but Zend have finally announced PHP5 certification. I purchased my voucher 3 months ago, now all I need is the study guide to materialise!

PHP4 Exceptions

Sunday, 10th September, 2006

During the last project I worked on, before moving companies, I would often want to pass back more error information from methods than primitive types would allow. What I wanted was a PHP4 exception. You’ll need PHP 4.3.0 or above for this class to work.

class Error {

    var $message;
    var $code;
    var $file;
    var $line;
    var $trace;

    function Error($message = null, $code = 0) {

        $this->message  = $message;
        $this->code     = $code;
        $this->trace    = (phpversion() >= 4.3) ? debug_backtrace() : array();

    }

    function getMessage() {

        return $this->message;

    }

    function getCode() {

        return $this->code;

    }

    function getFile() {

        return $this->trace['file'];

    }

    function getLine() {

        return $this->trace['line'];

    }

    function getTrace() {

        reset($this->trace);
        return current($this->trace);

    }

    function getArgs() {

        reset($this->trace);
        $trace = current($this->trace);
        return $trace['args'];

    }

}

I then created the following function to identify when the Error class was returned:

function isError($result) {

    return (is_a($result,'Error') ? true : false);

}

I would then use it like so:

class OrderDB {

    ...

    function save() {

        if (!$this->db->query(
            'UPDATE orders SET ... WHERE order_id = ' . $this->order->getId())) {
            return new Error('Failed to update order ' . $this->order->getId());
        } else {
            return true;
        }

    }

    ...

}

Mimicking a try catch block:

$saveResult = $orderDb->save();

if ($saveResult) {
    ...
} else if (isError($saveResult)) {
    $logger = $this->locator->get('log');
    $logger->log('Error occured in ' . $saveResult->getFile() . ' on line ' . $saveResult->getLine() . ': ' . $saveResult->getMessage());
    ...
}

An obvious short coming of this solution is that every result must be checked whereas a true try catch statement will catch any exception thrown within the try block. On the plus side you can still extend the Error class and isError will recognise you’ve returned a child of the Error class.

Typically 2 minutes Googling reveals a beefed up PHP4 exception class.

PHP 5 Certification Announced

Thursday, 27th July, 2006

Zend have finally posted some information about their new PHP5 certification. This new certification was planned to be released yesterday but has been postponed now until the end of August/early Sept.

There’s a fairly comprehensive list of topics – hopefully it’ll be a more worthwhile qualification than the original! No rushing the study guide this time please guys!

PHP exe compiler & GUI apps

Tuesday, 11th July, 2006

I just read about Bambalam PHP EXE Compiler/Embedder on phpdeveloper – very cool. Then I spotted WinBinder, genius! (I wonder how performant PHP is for GUI apps)

You are currently browsing greg's weblog archives in the PHP category.

Categories

xhtml 1.1 compliant   xhtml 1.1 compliant