下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、php面向?qū)ο蟪绦蛟O(shè)計之類與反射api詳解這篇文章主要介紹了 php面向?qū)ο蟪绦蛟O(shè)計之類與反射api,結(jié)合實例形式較為詳細(xì)的分析 了類的驗證、檢查、傳參及反射api等概念與操作技巧,需要的朋友可以參考下本文實例講述了 php面向?qū)ο蟪绦蛟O(shè)計之類與反射apl分享給大家供大家參考,具體如下:了解類class_exists驗證類是否存在<?php/ taskr unn er.php$class name =” task"$path = htasks/$class namephp"if (!file_exists( $path ) throw new exception( &
2、quot;no such file as $path"); 拋出異常,類文件不存 ffirequire_once( $path );$qclassname = htasks$classname”;if ( ! class_exists( $qclassname ) throw new exception( nno such class as $qclassnameh ); 拋出異常'類不存在 fatal error: un caught except! on 'exceptio n' with message 'no such class as tas
3、ks'task'stack trace:#0 main$myobj = new $qclassname();$myobj->dospeak();?> get_class檢查對彖的類instanceof驗證對象是否屈丁某個類<?phpclass cdproduct function getproductf) return new cdproduct( mexile on coldharbour lane","the", "alabama 3", 10.99, 60.33 ); / 返回一個類對象$product
4、=getproduct();if ( get_class( $product ) = 'cdproduct' ) print "$product is a cdproduct objectnn;?x?phpclass cdproduct jfunction getproduct() return new cdproduct( “exile on coldharbour lane”,"the", "alabama 3", 10.99, 60.33 );$product =getproduct();if ( $product ins
5、tanceof cdproduct ) print h$product is a cdproduct objectnn;?>get_class_methods得到類中所有的方法列表,只獲取public的方法,protected, private的方 法獲取不到。默認(rèn)的就是publico<?phpclass cdproduct fun ction _con struct) fun ctio n getplayle ngth() fun ction getsummaryline() function getproducerfirstname() function getproducer
6、mainname() function setdiscount() function getdiscount() function gettitle() function getprice() function getproducerf) print_r( get_class_methods( 'cdproduct*) );?> output:array( 0 => _con struct 1 => getplayle ngth 2 => getsummaryli ne 3=> getproducerfirstname 4 => getproduce
7、rmainname 5 => setdiscount 6 => getdiscount => gettitle 8 => getprice 9 => getproducer)更多驗證<?phpclass shopproduct interface incidental ;class cdproduct extends shopproduct implements incidentai public $coverurl; function _construct() function getplaylength() function getsummaryline
8、() function getproducerfirstname() function getproducermainname() function setdiscount() function getdiscount() function gettitle() return htitlen" function getprice() function getproducer() function getproduct() return new cdproduct();$product = getproduct(); / acquire an object$method = hgett
9、itleu; / define a method nameprint $product->$method(); / invoke the methodif (in_array( $method, get_class_methods( $product ) ) ) print $product->$method(); / invoke the methodif ( is_callable( array) $product, $method) ) ) print $product->$method(); / invoke the methodif ( method_exists(
10、 $product, $method ) ) print $product->$method(); / invoke the methodprint_r( get_class_vars( 'cdproduct1 ) );if ( is_subclass_of( $product, 'shopproduct* ) print ''cdproduct is a subclass of shopproductnh;if(is_subclass_of( $ product, 'in cide ntal' ) ) print hcdproduct i
11、s a subclass of in cide ntaln"訐 (in_array( 'in cidentaclassjmpleme nts( $product ) ) print "cdproduct is an in terface of in cide ntalrt'?> output: titletitletitletitlearrayf coverllrl =>)cdproduct is a subclass of shopproductcdproduct is a subclass of incide ntalcdproduct is
12、 an in terface of incidental_call方法<?phpclass othershop function thing() print hthingn”; function andanotherthingf) print "another thingnn; class delegator private $thirdpartyshop; function _construct() $this->thirdpartyshop = new othershopo; function _call( $method, $args ) / 當(dāng)調(diào)用未命名方法時執(zhí)行
13、 call 方法 if ( method_exists( $t h is->t h i rd pa rtysho p, $method ) ) return $this->thirdpartyshop->$method( ); $d = newdelegator();$d->thing();?>output:thing傳參使用<?phpclass othershop function thing() print hthi ngn" functionandanotherthing( $a, $b ) print "another thing
14、 ($a, $b)nh; class delegator private $thirdpartyshop; fun ctio n _construct() $t h is->t h i rd pa rtysho p = new othershop(); function _call( $method, $args ) if ( method_exists( $this->thirdpartyshop, $method ) return call_user_func_array(array) $this->thirdpartyshop,$method ), $args );$d
15、 = new delegator();$d->andanotherthing( "hi'1, "hello” );?> output:another thing (hi, hello)反射apifullshop.php<?phpclass shopproduct private $title; private $producermai nn ame;private$producerfirstname; protected $price; private $discount = 0; public function _construct( $titl
16、e, $firstname,$mainname, $price ) $this->title=$title;$this->produce rfirstname = $firstname;$this->producermai nn ame =$mainname;$this->price= $price; public function getproducerfirstname()return $this->producerfirstname; public function getproducermainname()return $this->producer
17、mainname; public function setdiscount( $num )$this->discount=$num; public function getdiscount() return $this->discount; public function gettitle() return $this->title; public function getprice() return ($this->price - $this->discount); public fun ctio n getproducer() return,l$this-&g
18、t;producerfirstnameh.h $this->producermainname" public functiongetsummaryline() $base = h$this->title ( $this->producermainname, h; $base = h$thjs->produce汗irstname )" return $base; class cdproduct extends shopproduct private $playlength = 0; public function _construct$title, $
19、firstname,$mai nn ame, sprice, $playl en gth=78 ) parent:_con struct) $title, $firstname,$mainname, $price ); $this->playlength = $playlength; public function getplaylength() retur n $this->playl eng th; public function getsummaryli ne() $base = parent:getsummaryline();$base 二 h: playing time
20、- $this->playlength"return$base; class bookproduct extends shopproduct private $numpages = 0; public function _construct( $title, $firstname,$mainname, $price, $numpages )parent:_construct( $title, $firstname,$mainname, $price );$this->numpages = $numpages; public function getnumberofpage
21、s() return$this->nu mpages; public function getsummaryli ne() $base =parent:getsummaryline(); $base 二 n: page count $this->numpages1'return $base; public function getprice() return $this->price; /*$productl = new cdproduct(hcdl,'/ "bob 蔦"bobbleson", 4, 50 );print $pro
22、ductl->getsummaryline()."nn;$product2 = new bookproductc'bookl","harry","harrelson",4,30);print$product2->getsummaryline()”rt'*/?>v?phprequire_once "fullshop.phph;$prod_class = new reflectionclass( 'cdproduct' );reflection:export( $prod_cla
23、ss );?> output:class <user> class cdproductexte ndsshopproduct d:xampphtdocspopp-code5fullshop.php 53-73 constants 0 - static properties 0 static methods 0 - properties 2 property <default> private $playlength property <default> protected $price methods 10 method <user, overw
24、rites shopproduct, ctor> public method _con struct d:xampphtdocspopp-code5fullshop.php 56 61parameters 5 parameter #0 vrequired> $title parameter #1 vrequired> $firstname parameter #2 <required> $mainname parameter #3 <required> $price parameter #4 <optiona卜 $playlength = 78
25、method <user> public methodgetplaylength d:xampphtdocspopp-code5fullshop.php 63 65 method<usec overwrites shopproduct, prototype shopproduct> public method getsummaryune d:xampphtdocspopp-code5fullshop.php 67 - 71 method <user, inheritsshopproduct> public methodgetproduce rfirstnam
26、ed:xampphtdocspopp-code5fullshop.php 17 - 19 method <user, inherits shopproduct>public method getproducermainname d:xampphtdocspopp-code5fullshop.php21 - 23 method <user, inherits shopproduct> public method setdiscount d:xampphtdocspopp-code5fullshop.php 25 27 parameters 1 parameter #0&l
27、t;required> $num method <user, inherits shopproduct> public method getdiscount d:xampphtdocspopp-code5fullshop.php 29 - 31 method <user, inheritsshopproduct> public method gettitle d:xampphtdocspopp-code5fullshop.php 33-35 method <user, inherits shopproduct> public method getpri
28、ce d:xampphtdocspopp-code5fullshop.php 37 39 method <useg inherits shopproduct>public method getproducer d:xampphtdocspopp-code5fullshop.php 41 - 44點(diǎn)評:把類看的透徹的一塌糊涂,比var_dump強(qiáng)多了。哪些屬性,繼承了什么類。類中的 方法哪些是自己的,哪些是重寫的,哪些是繼承的,一目了然。查看類數(shù)據(jù)<?phprequire_once(,lfullshop.php,);f un ction classdata( reflecti
29、on class $class ) $details = ,lfl; $name = $class->getname(); if ( $class->isuserdefined() ) $details 二 n$name is user definedn" 訐($class->islnternal() ) $details .= "$name is built-inn" 訐 ($class->islnterface() ) $details .= ”$name is interfacen" if ( $class->isab
30、stract() $details ="$name is an abstract classn”; if ( $class->isfinal() ) $details 二 ”$name is a final classn”; if ( $class->islnstantiable() ) $details = ”$name can be instantiatedn" else $details = ”$name can not be instantiatedrt; return $details;$prod_class = new reflecti on cla
31、ss) 'cdproduct' );pri nt classdata( $prod_class );?> output:cdproduct is user definedcdproduct can be instantiated查看方法數(shù)據(jù)<?phprequire_once ,fullshop.php,;$prod_class = new reflectionclass( cdproduct' );$methods = $prod_class->getmethods();foreach ( $methods as $method ) print met
32、hoddata( $method ); print nn-nu;function methoddata( reflectionmethod $method ) $details = lth; $name = $method>getname(); if ( $method>isuserdefined() ) $details =“$name is user definedn”; if ( $method->islnternal() ) $details .= *'$name is built-inn" if ( $method->isabstract()
33、 $details .= "$name is abstractn" if ( $method->ispublic() ) $details .= "$name is publicn" 訐($method->isprotected() ) $details .= "$name is protectednn; if ($method->isprivate() ) $details .= ”$name is privaten" if ( $method->isstatic() $details .= "$n
34、ame is staticn" if ( $method->isfinal() $details .= "$name is finaln" if ($method->isc on structor() ) $details 二"$n ame is the con structornh; 訐 ($method > returnsrefere nce() ) $details 二 ”$n ame retur ns a ref ere nee (as opposed to a value)nh; return $details;?> o
35、utput:_con struct is user defined_con struct is public_construct is the con structor-getplayle ngth is user definedgetplaylength is public-一getsummaryline is user definedgetsummaryline is public-一getproduce 汗 irstnameis user definedgetproducerfirstname ispublicgetproducermainname is user definedgetproducermainname is publicsetdiscount is user definedsetdiscount is publicgetdiscount is user definedgetdiscount is public-gettitle is user definedgettitle is public-getprice is user definedgetprice is public-getproducer is user define
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024冷庫租賃合同書范本(適用小型企業(yè))
- 2024年度互聯(lián)網(wǎng)廣告技術(shù)服務(wù)合同
- 2024年買賣合同標(biāo)的為新能源汽車
- 2024年度影視制作與發(fā)行承包合同
- 2024年度房地產(chǎn)商業(yè)綜合體建設(shè)項目施工合同
- 公租房個人收入證明(12篇)
- 2024年度安置房社區(qū)文化活動合同
- 手機(jī)教學(xué)課件教學(xué)
- 2024年度品牌合作框架協(xié)議
- 2024年度特許經(jīng)營合同標(biāo)的及許可使用范圍
- ISO9001體系文件與IRIS標(biāo)準(zhǔn)條款對應(yīng)表
- 漢語教師志愿者培訓(xùn)大綱
- 護(hù)理導(dǎo)論 評判性思維
- SPC培訓(xùn)資料_2
- 學(xué)習(xí)適應(yīng)性測驗(AAT)
- ADS創(chuàng)建自己的元件庫
- MATLAB仿真三相橋式整流電路(詳細(xì)完美)
- 2019年重慶普通高中會考通用技術(shù)真題及答案
- 天秤座小奏鳴曲,Libra Sonatine;迪安斯,Roland Dyens(古典吉他譜)
- 鋼筋混凝土工程施工及驗收規(guī)范最新(完整版)
- 光纜施工規(guī)范及要求
評論
0/150
提交評論