清单 6. 创建、缓存和检索 PHP 对象 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

<?php

 

class myCache extends Memcache {

function getInstance() {

static $instance;

 

if ( ! isset( $instance )) {

$instance = new Memcache;

$instance->connect( '198.168.0.1' );

}

 

return $instance;

}

 

function close( ) {

if ( isset( $instance ) ) {

$instance->close();

$instance = null;

}

}

}

 

class myDatum {

var $values = array(

'description' => null,

'price' => null,

'weight' => null

);

 

function myDatum( $settings ) {

foreach ( $settings as $key => $value ) {

if ( ! array_key_exists( $key, $this->values ) ) {

die( "Unknown attribute: $key" );

}

 

$this->values{ $key } = $value;

}

}

 

function set( $key=null, $value=null ) {

if ( is_null( $key ) ) {

die( "Key cannot be null" );

}

 

if ( ! array_key_exists( $key, $this->values ) ) {

die( "Unknown attribute: $key" );

}

 

if ( ! is_null( $value ) ) {

$this->values{ $key } = $value;

}

 

return( $this->values{ $key } );

}

 

function get( $key=null ) {

return( $this->set( $key, null ) );