adding methods to classes

From: Date: Sat, 07 Apr 2001 21:53:08 +0000
Subject: adding methods to classes
Groups: php.general 
Request: Send a blank email to php-general+get-47586@lists.php.net to get a copy of this message
Does PHP allow you to add methods to a class outside the class definition? This seems not to be straightforward since class prototypes are not allowed. For instance, what I'm trying to do is add a method to the DB class (in PEAR) that will do a database query, test if the result is an error/warning, trigger and error/warning, and (if possible) return the result. Something like: DB::ecQuery(&$db, $query) { $result = $db->query($query); if(DB::isError($result)) { triggerError(DB::errorMessage($result), E_USER_ERROR); } else if(DB::isWarning($result)) { triggerError(DB::errorMessage($result), E_USER_WARNING); } return $result; } I can't just add this to my own local copy of PEAR, because my app will be a public use app, and I'd like my users to have the convenience of using their preinstalled versions of PEAR (although I've thought of doing something like BinaryCloud, which includes a distribution of metabase in its distribution). Also, in case you're wondering, I'm triggering these errors b/c I know my app will appropriately set PHP error_reporting to a user-defined value. As it is, I have a "static" class called "Error" with a method called "dbQuery" that I'm using, but a class called "Error" doesn't seem appropriate -- "DB" does. Anybody know if this is possible? Thanks. Dean.

« previous php.general (#47586) next »