Il capitolo 12 di questo libro è scritto davvero molto male in quanto è pieno di errori.
Ho trovato un nuovo problema al codice numero 5 che consta di 2 file. In genere apro lo script provo a leggerlo e a cercare di indovinare l'output e poi lo lancio.
Secondo i miei calcoli dovrei leggere questo html:
codice HTML:
<h1>\woo\base\MemApplicationRegistry</h1>
1
<pre></pre>
<h1>\woo\base\SessionRegistry</h1>
1
<pre></pre>
mentre in realtà leggo questo: 
codice HTML:
<html><head></head><body><h1>\woo\base\MemApplicationRegistry</h1><br> <b>Fatal error</b>: Call to undefined function apc_fetch() in <b>C:\xampp\htdocs\9781430260318_Chapter_12_Code\listing12.05.php</b> on line <b>145</b><br> </body></html>
I 2 file sono questi:
listing12.05.php
Codice PHP:
<?php
namespace woo\base;
abstract class Registry {
abstract protected function get( $key );
abstract protected function set( $key, $val );
}
class RequestRegistry extends Registry {
private $values = array();
private static $instance=null;
private function __construct() {}
static function instance() {
if ( is_null(self::$instance) ) { self::$instance = new self(); }
return self::$instance;
}
protected function get( $key ) {
if ( isset( $this->values[$key] ) ) {
return $this->values[$key];
}
return null;
}
protected function set( $key, $val ) {
$this->values[$key] = $val;
}
static function getRequest() {
$inst = self::instance();
if ( is_null( $inst->get( "request" ) ) ) {
$inst->set('request', new \woo\controller\Request() );
}
return $inst->get( "request" );
}
}
class SessionRegistry extends Registry {
private static $instance=null;
private function __construct() {
session_start();
}
static function instance() {
if ( is_null(self::$instance) ) { self::$instance = new self(); }
return self::$instance;
}
protected function get( $key ) {
if ( isset( $_SESSION[__CLASS__][$key] ) ) {
return $_SESSION[__CLASS__][$key];
}
return null;
}
protected function set( $key, $val ) {
$_SESSION[__CLASS__][$key] = $val;
}
function setDSN( $dsn ) {
self::instance()->set('dsn', $dsn );
}
function getDSN( ) {
return self::instance()->get("dsn");
}
}
class ApplicationRegistry extends Registry {
private static $instance=null;
private $freezedir = "data";
private $values = array();
private $mtimes = array();
private $request=null;
private function __construct() { }
static function clean() {
self::$instance=null;
}
static function instance() {
if ( is_null(self::$instance) ) { self::$instance = new self(); }
return self::$instance;
}
protected function get( $key ) {
$path = $this->freezedir . DIRECTORY_SEPARATOR . $key;
if ( file_exists( $path ) ) {
clearstatcache();
$mtime=filemtime( $path );
if ( ! isset($this->mtimes[$key] ) ) { $this->mtimes[$key]=0; }
if ( $mtime > $this->mtimes[$key] ) {
$data = file_get_contents( $path );
$this->mtimes[$key]=$mtime;
return ($this->values[$key]=unserialize( $data ));
}
}
if ( isset( $this->values[$key] ) ) {
return $this->values[$key];
}
return null;
}
protected function set( $key, $val ) {
$this->values[$key] = $val;
$path = $this->freezedir . DIRECTORY_SEPARATOR . $key;
file_put_contents( $path, serialize( $val ) );
$this->mtimes[$key]=time();
}
static function getDSN() {
return self::instance()->get('dsn');
}
static function setDSN( $dsn ) {
return self::instance()->set('dsn', $dsn);
}
static function getRequest() {
$inst = self::instance();
if ( is_null( $inst->request ) ) {
$inst->request = new \woo\controller\Request();
}
return $inst->request;
}
}
class MemApplicationRegistry extends Registry {
private static $instance=null;
private $values=array();
private $id;
private function __construct() { }
static function instance() {
if ( is_null(self::$instance) ) { self::$instance = new self(); }
return self::$instance;
}
protected function get( $key ) {
return \apc_fetch( $key );
}
protected function set( $key, $val ) {
return \apc_store( $key, $val );
}
static function getDSN() {
return self::instance()->get("dsn");
}
static function setDSN( $dsn ) {
return self::instance()->set("dsn", $dsn);
}
}
?>
listing12.05.run.php
Codice PHP:
<?php
// put me in a web facing directory
// together with listing12.05.php
// then run in two windows -- confirm that an update in one
// window is reflected in the other next time it reloads
require_once( "listing12.05.php" ); // Registry
print "<h1>\woo\base\MemApplicationRegistry</h1>";
$reg = \woo\base\MemApplicationRegistry::instance();
if ( is_null( $reg->getDSN() ) ) {
$reg->setDSN(1);
}
print $reg->getDSN();
print "<pre>";
$reg->setDSN( $reg->getDSN()+1);
print "</pre>";
print "<h1>\woo\base\SessionRegistry</h1>";
$reg2 = \woo\base\SessionRegistry::instance();
if ( is_null( $reg2->getDSN() ) ) {
$reg2->setDSN(1);
}
print $reg2->getDSN();
print "<pre>";
$reg2->setDSN( $reg2->getDSN()+1);
print "</pre>";
?>