文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>高级PHP V5 对象研究

高级PHP V5 对象研究

时间:2007-02-18  来源:PHP爱好者

??PHP V5 ????

?????PHP V5??????????????????????????????????????????????????????????
??????
????????????????????????????????????????????????????????? Car ??? Car ????? Car ??? Vehicle ???? Car ??????? Vehicle ???????????????????????????????????????????????????????????????????????????????????????????
??“???? PHP V5 ????”????????????????? Dictionary ???????????? $translations ??? summarize() ?????????????????????????????????????????????? Dictionary ??????????? imposter???? instanceof ????????????????? PHP V5 ??????????????????????
?? instanceof Dictionary
????????????????? instanceof ??????????????????? Dictionary ???????????????????
if ( $en instanceof Dictionary ) {
?print $en->summarize();
}
????????? PHP V5 ???????????????????????
???“???? PHP V5 ????”??????????Dictionary?????????? DictionaryIO??? Dictionary ???????????????????????? Dictionary ???????????????????????????????????????????????????????? 1 ? Dictionary ???????????? DictionaryIO ???????????????
???? 1. ?? DictionaryIO ??? Dictionary ??????
class Dictionary {
?public $translations = array();
?public $type ="En";
?public $dictio;
?function addDictionaryIO( $dictio ) {
??$this->dictio=$dictio;
?}
?function export() {
??if ( $this->dictio ) {
???$this->dictio->export( $this );
??}
?}
}
class DictionaryIO {
?function export( $dict ) {
??print "exporting dictionary data "."($dict->type)n";
?}
}
$en = new Dictionary();
$en->addDictionaryIO( new DictionaryIO() );
$en->export();
// output:
// dumping dictionary data (En)
??DictionaryIO ??????? export()?????? Dictionary ?????????????????Dictionary ????????addDictionaryIO()?????? DictionaryIO ??? export()??????????? Dictionary ?? —— ?????????????
??????????? Dictionary ?????????? DictionaryIO ???????????????????????????????????????????? DictionaryIO ?????? Dictionary ??????????????????????????? DictionaryIO ????? Dictionary????????? ???????????? DictionaryIO ????? XmlDictionaryIO??????? Dictionary??????????????????
??? 1 ??? Dictionary ? DictionaryIO ????????


? 1. Dictionary ? DictionaryIO ?
????????????????????????????? addDictionaryIO()?????? export() ?????????????????????? $dictio ?????????? export() ????? PHP V4 ??????????????????????????????????? PHP V5 ???????????????????????????????????????????? 2 ???
???? 2. ??????????????????
function addDictionaryIO( DictionaryIO $dictio ) {
?$this->dictio=$dictio;
}
function export() {
?if ( $this->dictio ) {
??$this->dictio->export( $this );
?}
}
?????????????????????????? addDictionaryIO()?PHP ????????????????????????????????????????????????????????????????????????
???????? addDictionaryIO() ?????????????????????????export() ???? export() ??? $dictio ??????????????????????????? DictionaryIO ?????????????? $dictio ??????
????????
????? 3 ??XmlDictionaryIO ?? DictionaryIO?? DictionaryIO ???????????XmlDictionaryIO ?? XML??????????????XmlDictionaryIO ?????????import() ? export()????????????????path()????????? XmlDictionaryIO ???? path() ????? DictionaryIO ???? path() ??????
???????????????????????????????????????? parent????????????????????? parent ???????? XmlDictionaryIO ???????????????????? xml ????????????? DictionaryIO ???????????? 3 ???
???? 3. XmlDictionaryIO ?? xml ???? DictionaryIO ????????
class XmlDictionaryIO extends DictionaryIO {
?function path( Dictionary $dictionary, $ext ) {
??$sep = DIRECTORY_SEPARATOR;
??if ( is_dir( ".{$sep}xml" ) ) {
???return ".{$sep}xml{$sep}{$dictionary->getType()}.$ext";
??}
??return parent::path( $dictionary, $ext );
?}
// ...
?????????????? xml ??????????????? parent ??????????
???????????
??parent ??????????????????????????????? parent ??????????????????????????????????????????????????????? 4 ???
??Listing 4. Invoking the parent class’s constructor
class SpecialDictionary extends Dictionary {
?function __construct( $type, DictionaryIO $dictio, $additional ) {
??// do something with $additional
??parent::__construct( $type, $dictio );
?}
}

??????
??????????????????????????????????????????????????????????? import() ? export()???? broken ?????????DictionaryIO ??????????????XmlDictionaryIO ?? DictionaryIO ????????????????
??PHP V5 ????????????????????????????????????????
abstract class DictionaryIO {}
??????????????????????????????????????????????????????????? 5 ?????????? abstract ???????????????????????????????? abstract ??????????????function ??????????????????????????????
???? 5. ?????
abstract class DictionaryIO {
protected function path( Dictionary $dictionary,
$ext ) {
$path = Dictionary::getSaveDirectory();
$path .= DIRECTORY_SEPARATOR;
$path .= $dictionary->getType().".$ext";
return $path;
}
abstract function import( Dictionary $dictionary );
abstract function export( Dictionary $dictionary );
}
?????path() ??????????????????????????? DictionaryIO ?????????? DictionaryIO ???????? import() ? export() ???????????????
?????????????????????????????????????????????????????????
???? 6 ?????? DictionaryIO ?????????????????
???? 6. ??? DictionaryIO ?
class SerialDictionaryIO extends DictionaryIO {
?function export( Dictionary $dictionary ) {
??// implementation
?}
?function import( Dictionary $dictionary ) {
??// implementation
?}
}
class XmlDictionaryIO extends DictionaryIO {
?protected function path( Dictionary $dictionary, $ext ) {
??$path = strtolower(parent::path( $dictionary, $ext ) );
??return $path;
?}
?function export( Dictionary $dictionary ) {
??// implementation
?}
?function import( Dictionary $dictionary ) {
??// implementation
?}
}
??Dictionary ????? DictionaryIO ???????????????????????????? XmlDictionaryIO ? SerialDictionaryIO ????????????????? DictionaryIO??????????? import() ? export() ??????????????????????????????????
??? 2 ??? DictionaryIO ???????????????????????????????????? DictionaryIO ????????? DictionaryIO?? SerialDictionaryIO ? XmlDictionaryIO ???????


? 2. ?? DictionaryIO ???????
??
??? Java? ???????????PHP ???????????????????????????????????????????????????clean design???????????????????????
??????????????????????????Dictionary ?????? get() ???????? Dictionary ???????????Dictionary ????????? export() ?????????????????????????????????????????????????????????? export() ??????????????????????????? export()??? 7 ????? export() ????????
???? 7. ?? export() ???????
class ThirdPartyNews {
// ...
}
class OurNews extends ThirdPartyNews {
// ...
function export() {
print "OurNews exportn";
}
}
??????????????? OurNews ?????? ThirdPartyNews ?????
???? 8 ?????? export() ???????????
???? 8. ??? export() ??????????
class Exporter {
?private $exportable = array();
?function add( $obj ) {
??$this->exportable[] = $obj;
?}
?function exportAll() {
??foreach ( $this->exportable as $obj ) {
???$obj->export();
??}
?}
}
??Exporter ?????????add()??????????? exportAll()??????????????????? export()?????????????add() ?????????????? exportAll() ?????? export() ????????? ???????? add() ?????????????Dictionary ? OurNews ????????????? add() ???????????????????????????? export() ???????????????????
????????????????????????????????? interface ????????
interface Exportable {
public function export();
}
?????????????????????????????????????????????????????????????????????????????????????? ?? implements ??????????? 9 ???
???? 9. ? implements ?????????
class OurNews extends ThirdPartyNews
implements Exportable {
?// ...
?function export() {
??print "OurNews exportn";
?}
}
class Dictionary implements Exportable, Iterator {
?function export() {
??//...
?}
}
????? implements ??????????????????????????????????????????????????? ??????????????Dictionary ? OurNews ??????????????? Exportable????????? instanceof ?????????? 10 ??????? Exporter::add() ???
???? 10. ???? Exporter::add() ??
class Exporter {
?private $exportable = array();
?function add( Exportable $obj ) {
??$this->exportable[] = $obj;
?}
//...
?????????????????????????????????????????????????????????????????????????????????????????????????? Exportable ?????????????? export() ???????????????????????
??? 3 ??? Exportable ???????????????? Exporter ? Exportable ????????????????????????????


? 3. ???????
?????
???????? PHP V5 ??????????????????????????????????????????????????????????????? ?????????????????????????????????????????????????Dictionary ???????????????? XML?????????????Dictionary ?????????????????????????????????????????????????Dictionary ?????????? DictionaryIO ??????? export() ? import() ????
?????????????????????? instanceof ???????????????????????????????????????????????????????
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载