Monday, April 11, 2011

PHPUnit and global variable

"PDOException: You cannot serialize or unserialize PDO instances"

If you use phpunit and encounter this message above, you are hit by phpunit nature.
PHPUnit by default serialize all object in global.
So if you have a pdo connection as global variable, or as a variable / properties of a global variable, then you will doomed to face this problem.

Solution:
If you have a class with a property of type xpdo,
create a function __sleep()
and return variable names in array without the xpdo property:
Example:
public function __sleep() {
$aKey = array_keys(get_object_vars($this));

$aResult = array();
foreach($aKey as $sKey) {
if ($sKey != "xpdo") $aResult[] = $sKey;
}
return $aResult;
}

No comments: