Home  >  Article  >  Backend Development  >  Summary of code examples of some classes in PHP object-oriented

Summary of code examples of some classes in PHP object-oriented

伊谢尔伦
伊谢尔伦Original
2017-06-30 09:51:29901browse

了解类

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 &#39;Exception&#39; with message &#39;No such class as tasks\Task&#39;
Stack trace:
#0 {main}
}
$myObj = new $qclassname();
$myObj->doSpeak();
?>

get_class 检查对象的类 instanceof 验证对象是否属于某个类

<?php
class CdProduct {}
function getProduct() {
  return new CdProduct(  "Exile on Coldharbour Lane",
                "The", "Alabama 3", 10.99, 60.33 ); // 返回一个类对象
}
$product = getProduct();
if ( get_class( $product ) == &#39;CdProduct&#39; ) {
  print "\$product is a CdProduct object\n";
}
?>
<?php
class 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。

<?php
class 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( &#39;CdProduct&#39; ) );
?>

output:

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

更多验证

<?php
class 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 name
print $product->$method(); // invoke the method
if ( 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( &#39;CdProduct&#39; ) );
if ( is_subclass_of( $product, &#39;ShopProduct&#39; ) ) {
  print "CdProduct is a subclass of ShopProduct\n";
}
if ( is_subclass_of( $product, &#39;incidental&#39; ) ) {
  print "CdProduct is a subclass of incidental\n";
}
if ( in_array( &#39;incidental&#39;, class_implements( $product )) ) {
  print "CdProduct is an interface of incidental\n";
}
?>

output:

title
title
title
title
Array
(
  [coverUrl] =>
)
CdProduct is a subclass of ShopProduct
CdProduct is a subclass of incidental
CdProduct is an interface of incidental

call方法

<?php
class 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

传参使用

<?php
class 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)

The above is the detailed content of Summary of code examples of some classes in PHP object-oriented. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn