3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Serializer\Adapter
;
12 use Zend\Serializer\Exception
;
13 use Zend\Stdlib\ErrorHandler
;
15 class PhpCode
extends AbstractAdapter
18 * Serialize PHP using var_export
23 public function serialize($value)
25 return var_export($value, true);
29 * Deserialize PHP string
31 * Warning: this uses eval(), and should likely be avoided.
35 * @throws Exception\RuntimeException on eval error
37 public function unserialize($code)
39 ErrorHandler
::start(E_ALL
);
41 // This suppression is due to the fact that the ErrorHandler cannot
42 // catch syntax errors, and is intentionally left in place.
43 $eval = @eval
('$ret=' . $code . ';');
44 $err = ErrorHandler
::stop();
46 if ($eval === false ||
$err) {
50 // Error handler doesn't catch syntax errors
51 if ($eval === false) {
52 $lastErr = error_get_last();
53 $msg .= ': ' . $lastErr['message'];
56 throw new Exception\
RuntimeException($msg, 0, $err);