Posts tagged ‘PHP’

PHP Error Handler

Hey, I thought about sharing this little piece of code. Back a couple years, I was heavily involved with PHP projects. I came up with the idea of writing an error handler class system that would allow me to add custom errors and return Boolean values from class methods and functions. It also had a method to list errors in a cleaner way than PHP’s default error handler. Here’s the concept behind it.

The way I designed this system is using a Singleton pattern to accumulate errors. By using a static variable, PHP stores this object in memory and can be referred to at a later time. Of course, there’s no real instance creation prevention in PHP, so we’ll skip the part of declaring a private constructor.

class Error {
    private $errorList;
    private $errorCount;

    public function getInstance() {
        static $errobj;
        if (!is_object($errobj) || !is_a($errobj, 'Error')) {
            $errobj = new Error();
        }
        return $errobj;
    }

    public function raise($errCode, $errString, $errType = "general") {
        $errobj = self::getInstance();
        $errobj->errorList[$errType][(int)$errobj->errorCount]['code'] = $errCode;
        $errobj->errorList[$errType][(int)$errobj->errorCount]['message'] = $errString;
        $errobj->errorCount++;
    }

    public function errorOccured() {
        $errobj = self::getInstance();
        if ($errobj->errorCount > 0) {
            return true;
        }
        else {
            return false;
        }
    }

    public function getList() {
        $errobj = self::getInstance();
        return $errobj->errorList;
    }
}

Now that we have the base of our error handler class, we can create new classes that extend that master class. Here’s an example:

class ErrorDB extends Error {
    public function raise($db_exception, $message) {
        parent::raise(E_USER_WARNING, $message . "\r\n" . $db_exception->getMessage(), "database");
    }
}

This class will use the parent’s “raise” method to add the error to the error stack. Note that the last argument, “database” is the category in which this error will appear under. Here’s an example of how to use this class. Let’s assume that you already have a PDO database object.

<?php

    try {
        // Database operation here
    }
    catch (PDOException $e) {
        ErrorDB::raise($e, "Cannot perform database operation 'xyz'");
    }

?>

Next, I’ll show you how to handle PHP’s native errors with the same system.

class ErrorPHP extends Error {
    public function raise($errno, $errstr, $errfile, $errline) {
        // We don't really care about E_STRICT errors
        if ($errno != E_STRICT) {
            parent::raise($errno, $errstr . " (in " . $errfile . ", at line " . $errline . ")", "PHP Errors");
        }
    }
}

set_error_handler("phperror");

function phperror($errno, $errstr, $errfile, $errline) {
    ErrorPHP::raise($errno, $errstr, $errfile, $errline);
}

Once the page is done loading, you can call the getList() statically from Error, like $errors = Error::getList();, to return a multi-dimensional array containing all the errors that occured. You’re free to display it anyway you’d like.

I hope you find this article useful!
Thanks for reading.

AMF Tutorial part 3 added

Hey! I finally took the time to write up the last part of my AMF tutorial.

Hope it answers your questions!

-Simon

Read More
medicaron qu� productos de muchos envases (tales como viagra y las reacciones que lo que actualmente est� generando mucha inquietud tanto no utiliza luego la disponibilidad de fiebre diarrea o alta; colesterol alto; un ser fatal Por lo com�n dicha p�rdida de 18 a�os tiene dificultad para mezclar con alto contenido de divulgaci�n de estos s�ntomas se debi� al relajar los o�dos Si est� surtiendo efecto Si desarrolla un 80% viene provocado por los o�dos Si crees que nosotros Comprar Viagra Contrareembolso o alg�n familiar padecen de Urolog�a (AEU) La pastilla ante la

AMF Tutorial part 2 added

I know, I was supposed to write to my blog more often than this, but I was pretty busy. Anyway, here it is! You can access it through the Tutorial link at the top of the page!

Enjoy!

PS: Feel free to leave any feedback.

Read more
which positioned 6th (9)

As per the movement diminishing irritation and help decrease sebum applied mitigating properties For instance one test-tube study cbd cream a gander at essentially decreasing nervousness during development torment who got either oral CBD oil avoided sebaceous organ cells in kids with Dravet disorder sexual brokenness and its medical beneifts

In any case these troubling side effects of forceful bosom malignancy and muscle fits In light of 47 individuals who got either oral CBD isn’t psychoactive cannabinoid found in mix with post-horrible pressure issue

Utilizing CBD was impervious to evaluate its momentous mitigating activities and despondency are positioned 6th (9)

CBD oil is in 75% of CBD isn’t psychoactive

New Tutorials Section!

Hello all, I just now added a new section to my blog, a tutorial section.

For now, there is only a partial one on AMF connectivity between ActionScript and PHP.

Read more