Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

form values PHP5

// Assume $_GET['testy'] exists

import_request_variables("G", "user_");

if (isset($user_testy)) {
echo 'The value of the \'testy\' field is:' . $user_testy;
}

PHP5 __sleep to handle superclasses' private members

PHP5 introduces visibility for object/class methods and data members. This can prove problematic when serializing objects because base classes cannot see their parents' private vars, so these can't be serialized. Plus, a base class probably doesn't know about all of its parents' private vars (even its public and protected ones). So, I use this:


class Object {
    function __sleep() {
        return array_keys( (array)$this );
    }
}

class BaseClass extends Object {
    private $foo;
    private $dbh; // some resource, like a database handler
    function __sleep() {
        unset($this->dbh);
        return parent::__sleep();
    }
}

class SubClass extends BaseClass {
    private $bar;
    function __sleep() {
        return parent::__sleep();
    }
}



The point is, the subclasses do any local cleanup then delegate the actual __sleep request to a parent class. With a common parent class Object that all other classes extend, we can keep the real serialize code in one place but still allow children to change their state before sleeping.
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS