php物件導向中一些類的代碼執行個體匯總

來源:互聯網
上載者:User
瞭解類

class_exists驗證類是否存在

<?php// TaskRunner.php$classname = "Task";$path = "tasks/{$classname}.php";if ( ! file_exists( $path ) ) {  throw new Exception( "No such file as {$path}" ); //拋出異常,類檔案不存在}require_once( $path );$qclassname = "tasks\\$classname";if ( ! class_exists( $qclassname ) ) {  throw new Exception( "No such class as $qclassname" ); //拋出異常,類不存在Fatal error: Uncaught exception 'Exception' with message 'No such class as tasks\Task'Stack trace:#0 {main}}$myObj = new $qclassname();$myObj->doSpeak();?>

get_class 檢查對象的類 instanceof 驗證對象是否屬於某個類

<?phpclass CdProduct {}function getProduct() {  return new CdProduct(  "Exile on Coldharbour Lane",                "The", "Alabama 3", 10.99, 60.33 ); // 返回一個類對象}$product = getProduct();if ( get_class( $product ) == 'CdProduct' ) {  print "\$product is a CdProduct object\n";}?><?phpclass CdProduct {}function getProduct() {  return new CdProduct(  "Exile on Coldharbour Lane",                "The", "Alabama 3", 10.99, 60.33 );}$product = getProduct();if ( $product instanceof CdProduct ) {  print "\$product is a CdProduct object\n";}?>

get_class_methods 得到類中所有的方法列表,只擷取public的方法,protected,private的方法擷取不到。預設的就是public。

<?phpclass CdProduct {  function construct() { }  function getPlayLength() { }  function getSummaryLine() { }  function getProducerFirstName() { }  function getProducerMainName() { }  function setDiscount() { }  function getDiscount() { }  function getTitle() { }  function getPrice() { }  function getProducer() { }}print_r( get_class_methods( 'CdProduct' ) );?>

output:

Array(  [0] => construct  [1] => getPlayLength  [2] => getSummaryLine  [3] => getProducerFirstName  [4] => getProducerMainName  [5] => setDiscount  [6] => getDiscount  [7] => getTitle  [8] => getPrice  [9] => getProducer)

更多驗證

<?phpclass ShopProduct {}interface incidental {};class CdProduct extends ShopProduct implements incidental {  public $coverUrl;  function construct() { }  function getPlayLength() { }  function getSummaryLine() { }  function getProducerFirstName() { }  function getProducerMainName() { }  function setDiscount() { }  function getDiscount() { }  function getTitle() { return "title\n"; }  function getPrice() { }  function getProducer() { }}function getProduct() {  return new CdProduct();}$product = getProduct(); // acquire an object$method = "getTitle";   // define a method nameprint $product->$method(); // invoke the methodif ( in_array( $method, get_class_methods( $product ) ) ) {  print $product->$method(); // invoke the method}if ( is_callable( array( $product, $method) ) ) {  print $product->$method(); // invoke the method}if ( method_exists( $product, $method ) ) {  print $product->$method(); // invoke the method}print_r( get_class_vars( 'CdProduct' ) );if ( is_subclass_of( $product, 'ShopProduct' ) ) {  print "CdProduct is a subclass of ShopProduct\n";}if ( is_subclass_of( $product, 'incidental' ) ) {  print "CdProduct is a subclass of incidental\n";}if ( in_array( 'incidental', class_implements( $product )) ) {  print "CdProduct is an interface of incidental\n";}?>

output:

titletitletitletitleArray(  [coverUrl] =>)CdProduct is a subclass of ShopProductCdProduct is a subclass of incidentalCdProduct is an interface of incidental

call方法

<?phpclass OtherShop {  function thing() {    print "thing\n";  }  function andAnotherthing() {    print "another thing\n";  }}class Delegator {  private $thirdpartyShop;  function construct() {    $this->thirdpartyShop = new OtherShop();  }  function call( $method, $args ) { // 當調用未命名方法時執行call方法    if ( method_exists( $this->thirdpartyShop, $method ) ) {      return $this->thirdpartyShop->$method( );    }  }}$d = new Delegator();$d->thing();?>

output:

thing

傳參使用

<?phpclass OtherShop {  function thing() {    print "thing\n";  }  function andAnotherthing( $a, $b ) {    print "another thing ($a, $b)\n";  }}class Delegator {  private $thirdpartyShop;  function construct() {    $this->thirdpartyShop = new OtherShop();  }  function call( $method, $args ) {    if ( method_exists( $this->thirdpartyShop, $method ) ) {      return call_user_func_array(            array( $this->thirdpartyShop,              $method ), $args );    }  }}$d = new Delegator();$d->andAnotherThing( "hi", "hello" );?>

output:

another thing (hi, hello)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.