http://www.ibm.com/developerworks/xml/library/os-php-flexobj/ PHP5引入的面向对象的编程特性显著的提升了PHP语言的层次。不只是成员和方法的访问控制private, protected, public -- 和Java, C++, 或C#一样 -- 你同时还能创建运行期间能动态改变的对象,
http://www.ibm.com/developerworks/xml/library/os-php-flexobj/
PHP5引入的面向对象的编程特性显著的提升了PHP语言的层次。不只是成员和方法的访问控制private, protected, public -- 和Java, C++, 或C#一样 -- 你同时还能创建运行期间能动态改变的对象,动态的创建一个新的方法和属性。这些是Java, C++, 或C#语言无法提供的。这种语言能力使得类似于ROR这样的快速开发框架变得可能。
魔法函数是特定命名的方法,这个方法由PHP解释器在脚本执行的特定节点所触发。
最常见的魔法函数是对象创建时调用的构造函数。
除了__call方法, 其他魔法函数,如__get 和 __set, 是在引用不存在的实例变量的时候被调用。记住这一点,你可以开始写适用于任何数据库表访问的类了。
(以下主要是代码重构,保持原文)
<?php require_once("DB.php"); $dsn = 'mysql://root:password@localhost/bookdb'; $db =& DB::Connect( $dsn, array() ); if (PEAR::isError($db)) { die($db->getMessage()); } class Book { private $book_id; private $title; private $author; private $publisher; function __construct() { } function set_title( $title ) { $this->title = $title; } function get_title( ) { return $this->title; } function set_author( $author ) { $this->author = $author; } function get_author( ) { return $this->author; } function set_publisher( $publisher ) { $this->publisher = $publisher; } function get_publisher( ) { return $this->publisher; } function load( $id ) { global $db; $res = $db->query( "SELECT * FROM book WHERE book_id=?", array( $id ) ); $res->fetchInto( $row, DB_FETCHMODE_ASSOC ); $this->book_id = $id; $this->title = $row['title']; $this->author = $row['author']; $this->publisher = $row['publisher']; } function insert() { global $db; $sth = $db->prepare( 'INSERT INTO book ( book_id, title, author, publisher ) VALUES ( 0, ?, ?, ? )' ); $db->execute( $sth, array( $this->title, $this->author, $this->publisher ) ); $res = $db->query( "SELECT last_insert_id()" ); $res->fetchInto( $row ); return $row[0]; } function update() { global $db; $sth = $db->prepare( 'UPDATE book SET title=?, author=?, publisher=? WHERE book_id=?' ); $db->execute( $sth, array( $this->title, $this->author, $this->publisher, $this->book_id ) ); } function delete() { global $db; $sth = $db->prepare( 'DELETE FROM book WHERE book_id=?' ); $db->execute( $sth, array( $this->book_id ) ); } function delete_all() { global $db; $sth = $db->prepare( 'DELETE FROM book' ); $db->execute( $sth ); } } $book = new Book(); $book->delete_all(); $book->set_title( "PHP Hacks" ); $book->set_author( "Jack Herrington" ); $book->set_publisher( "O'Reilly" ); $id = $book->insert(); echo ( "New book id = $id\n" ); $book2 = new Book(); $book2->load( $id ); echo( "Title = ".$book2->get_title()."\n" ); $book2->delete( ); ?>
<?php require_once("DB.php"); $dsn = 'mysql://root:password@localhost/bookdb'; $db =& DB::Connect( $dsn, array() ); if (PEAR::isError($db)) { die($db->getMessage()); } class Book { private $book_id; private $fields = array(); function __construct() { $this->fields[ 'title' ] = null; $this->fields[ 'author' ] = null; $this->fields[ 'publisher' ] = null; } function __call( $method, $args ) { if ( preg_match( "/set_(.*)/", $method, $found ) ) { if ( array_key_exists( $found[1], $this->fields ) ) { $this->fields[ $found[1] ] = $args[0]; return true; } } else if ( preg_match( "/get_(.*)/", $method, $found ) ) { if ( array_key_exists( $found[1], $this->fields ) ) { return $this->fields[ $found[1] ]; } } return false; } function load( $id ) { global $db; $res = $db->query( "SELECT * FROM book WHERE book_id=?", array( $id ) ); $res->fetchInto( $row, DB_FETCHMODE_ASSOC ); $this->book_id = $id; $this->set_title( $row['title'] ); $this->set_author( $row['author'] ); $this->set_publisher( $row['publisher'] ); } function insert() { global $db; $sth = $db->prepare( 'INSERT INTO book ( book_id, title, author, publisher ) VALUES ( 0, ?, ?, ? )' ); $db->execute( $sth, array( $this->get_title(), $this->get_author(), $this->get_publisher() ) ); $res = $db->query( "SELECT last_insert_id()" ); $res->fetchInto( $row ); return $row[0]; } function update() { global $db; $sth = $db->prepare( 'UPDATE book SET title=?, author=?, publisher=? WHERE book_id=?' ); $db->execute( $sth, array( $this->get_title(), $this->get_author(), $this->get_publisher(), $this->book_id ) ); } function delete() { global $db; $sth = $db->prepare( 'DELETE FROM book WHERE book_id=?' ); $db->execute( $sth, array( $this->book_id ) ); } function delete_all() { global $db; $sth = $db->prepare( 'DELETE FROM book' ); $db->execute( $sth ); } } ..
<?php require_once("DB.php"); $dsn = 'mysql://root:password@localhost/bookdb'; $db =& DB::Connect( $dsn, array() ); if (PEAR::isError($db)) { die($db->getMessage()); } class DBObject { private $id = 0; private $table; private $fields = array(); function __construct( $table, $fields ) { $this->table = $table; foreach( $fields as $key ) $this->fields[ $key ] = null; } function __call( $method, $args ) { if ( preg_match( "/set_(.*)/", $method, $found ) ) { if ( array_key_exists( $found[1], $this->fields ) ) { $this->fields[ $found[1] ] = $args[0]; return true; } } else if ( preg_match( "/get_(.*)/", $method, $found ) ) { if ( array_key_exists( $found[1], $this->fields ) ) { return $this->fields[ $found[1] ]; } } return false; } function load( $id ) { global $db; $res = $db->query( "SELECT * FROM ".$this->table." WHERE ". $this->table."_id=?", array( $id ) ); $res->fetchInto( $row, DB_FETCHMODE_ASSOC ); $this->id = $id; foreach( array_keys( $row ) as $key ) $this->fields[ $key ] = $row[ $key ]; } function insert() { global $db; $fields = $this->table."_id, "; $fields .= join( ", ", array_keys( $this->fields ) ); $inspoints = array( "0" ); foreach( array_keys( $this->fields ) as $field ) $inspoints []= "?"; $inspt = join( ", ", $inspoints ); $sql = "INSERT INTO ".$this->table." ( $fields ) VALUES ( $inspt )"; $values = array(); foreach( array_keys( $this->fields ) as $field ) $values []= $this->fields[ $field ]; $sth = $db->prepare( $sql ); $db->execute( $sth, $values ); $res = $db->query( "SELECT last_insert_id()" ); $res->fetchInto( $row ); $this->id = $row[0]; return $row[0]; } function update() { global $db; $sets = array(); $values = array(); foreach( array_keys( $this->fields ) as $field ) { $sets []= $field.'=?'; $values []= $this->fields[ $field ]; } $set = join( ", ", $sets ); $values []= $this->id; $sql = 'UPDATE '.$this->table.' SET '.$set. ' WHERE '.$this->table.'_id=?'; $sth = $db->prepare( $sql ); $db->execute( $sth, $values ); } function delete() { global $db; $sth = $db->prepare( 'DELETE FROM '.$this->table.' WHERE '. $this->table.'_id=?' ); $db->execute( $sth, array( $this->id ) ); } function delete_all() { global $db; $sth = $db->prepare( 'DELETE FROM '.$this->table ); $db->execute( $sth ); } } $book = new DBObject( 'book', array( 'author', 'title', 'publisher' ) ); $book->delete_all(); $book->set_title( "PHP Hacks" ); $book->set_author( "Jack Herrington" ); $book->set_publisher( "O'Reilly" ); $id = $book->insert(); echo ( "New book id = $id\n" ); $book->set_title( "Podcasting Hacks" ); $book->update(); $book2 = new DBObject( 'book', array( 'author', 'title', 'publisher' ) ); $book2->load( $id ); echo( "Title = ".$book2->get_title()."\n" ); $book2->delete( ); ? >
.. class Book extends DBObject { function __construct() { parent::__construct( 'book', array( 'author', 'title', 'publisher' ) ); } } $book = new Book( ); $book->delete_all(); $book->{'title'} = "PHP Hacks"; $book->{'author'} = "Jack Herrington"; $book->{'publisher'} = "O'Reilly"; $id = $book->insert(); echo ( "New book id = $id\n" ); $book->{'title'} = "Podcasting Hacks"; $book->update(); $book2 = new Book( ); $book2->load( $id ); echo( "Title = ".$book2->{'title'}."\n" ); $book2->delete( ); ?>
<?php require_once("DB.php"); $dsn = 'mysql://root:password@localhost/bookdb'; $db =& DB::Connect( $dsn, array() ); if (PEAR::isError($db)) { die($db->getMessage()); } class DBObject { private $id = 0; private $table; private $fields = array(); function __construct( $table, $fields ) { $this->table = $table; foreach( $fields as $key ) $this->fields[ $key ] = null; } function __get( $key ) { return $this->fields[ $key ]; } function __set( $key, $value ) { if ( array_key_exists( $key, $this->fields ) ) { $this->fields[ $key ] = $value; return true; } return false; } function load( $id ) { global $db; $res = $db->query( "SELECT * FROM ".$this->table." WHERE ". $this->table."_id=?", array( $id ) ); $res->fetchInto( $row, DB_FETCHMODE_ASSOC ); $this->id = $id; foreach( array_keys( $row ) as $key ) $this->fields[ $key ] = $row[ $key ]; } function insert() { global $db; $fields = $this->table."_id, "; $fields .= join( ", ", array_keys( $this->fields ) ); $inspoints = array( "0" ); foreach( array_keys( $this->fields ) as $field ) $inspoints []= "?"; $inspt = join( ", ", $inspoints ); $sql = "INSERT INTO ".$this->table. " ( $fields ) VALUES ( $inspt )"; $values = array(); foreach( array_keys( $this->fields ) as $field ) $values []= $this->fields[ $field ]; $sth = $db->prepare( $sql ); $db->execute( $sth, $values ); $res = $db->query( "SELECT last_insert_id()" ); $res->fetchInto( $row ); $this->id = $row[0]; return $row[0]; } function update() { global $db; $sets = array(); $values = array(); foreach( array_keys( $this->fields ) as $field ) { $sets []= $field.'=?'; $values []= $this->fields[ $field ]; } $set = join( ", ", $sets ); $values []= $this->id; $sql = 'UPDATE '.$this->table.' SET '.$set. ' WHERE '.$this->table.'_id=?'; $sth = $db->prepare( $sql ); $db->execute( $sth, $values ); } function delete() { global $db; $sth = $db->prepare( 'DELETE FROM '.$this->table.' WHERE '. $this->table.'_id=?' ); $db->execute( $sth, array( $this->id ) ); } function delete_all() { global $db; $sth = $db->prepare( 'DELETE FROM '.$this->table ); $db->execute( $sth ); } } class Book extends DBObject { function __construct() { parent::__construct( 'book', array( 'author', 'title', 'publisher' ) ); } } $book = new Book( ); $book->delete_all(); $book->{'title'} = "PHP Hacks"; $book->{'author'} = "Jack Herrington"; $book->{'publisher'} = "O'Reilly"; $id = $book->insert(); echo ( "New book id = $id\n" ); $book->{'title'} = "Podcasting Hacks"; $book->update(); $book2 = new Book( ); $book2->load( $id ); echo( "Title = ".$book2->{'title'}."\n" ); $book2->delete( ); ?>