当前位置:Gxlcms > PHP教程 > PHP对象、模式与实践之高级特性案例分析

PHP对象、模式与实践之高级特性案例分析

时间:2021-07-01 10:21:17 帮助过:16人阅读

这篇文章主要介绍了PHP对象、模式与实践之高级特性,结合实例形式分析了php面向对象程序设计中的静态属性和方法、抽象类、接口、拦截器、克隆对象等概念与简单实现方法,需要的朋友可以参考下

具体如下:

高级特性

包括:

1.静态方法和属性(通过类而不是对象来访问数据和功能)
2.抽象类和接口(设计,实现分离)
3.错误处理(异常)
4.Final类和方法(限制继承)
5.拦截器(自动委托)
6.析构方法(对象销毁前的清理工作)
7.克隆对象(创建对象的副本)
8.把对象解析成字符串

PS,学会从内存的角度看代码。想象计算机的微观世界。

静态方法的小例子

  1. <?php
  2. class StaticExample{
  3. static public $aNum = 10;
  4. static public function sayHello(){
  5. print "hello";
  6. }
  7. }
  8. print StaticExample::$aNum."<br/>";
  9. StaticExample::sayHello();

tips:

1.静态方法不能访问类中的普通属性,因为那些属性属于一个对象,但可以访问静态属性。
2.我们不能再对象中调用静态方法,因此不能再静态方法中使用伪变量$this。

静态方法的大例子

  1. <?php
  2. class ShopProduct{
  3. private $title;
  4. private $producerMainName;
  5. private $producerFirstName;
  6. protected $price;
  7. private $discount = 0;
  8. private $id = 0;
  9. function __construct($title,$firstName,$mainName,$price){
  10. $this->title = $title;
  11. $this->producerFirstName = $firstName;
  12. $this->producerMainName = $mainName;
  13. $this->price = $price;
  14. }
  15. public function setID($id){
  16. $this->id = $id;
  17. }
  18. public static function getInstance($id,PDO $pdo){
  19. $query = "select * from products where id= '$id'";
  20. $stmt = $pdo->query($query);
  21. $row = $stmt->fetch();
  22. if(empty($row)){
  23. return null;
  24. }
  25. if($row['type'] == "book"){
  26. $product = new BookProduct($row['title'],
  27. $row['firstname'],
  28. $row['mainname'],
  29. $row['price'],
  30. $row['numpages']
  31. );
  32. }else if($row['type'] == "cd"){
  33. $product = new CdProduct($row['title'],
  34. $row['firstname'],
  35. $row['mainname'],
  36. $row['price'],
  37. $row['playLength']
  38. );
  39. }else{
  40. $product = new ShopProduct($row['title'],
  41. $row['firstname'],
  42. $row['mainname'],
  43. $row['price']
  44. );
  45. }
  46. $product->setId($row['id']);
  47. $product->setDiscount($row['discount']);
  48. return $product;
  49. }
  50. public function getProducerFirstName(){
  51. return $this->producerFirstName;
  52. }
  53. public function getProducerMainName(){
  54. return $this->producerMainName;
  55. }
  56. public function setDiscount($num){
  57. $this->discount = $num;
  58. }
  59. public function getDiscount(){
  60. return $this->discount;
  61. }
  62. public function getTitle(){
  63. return $this->title;
  64. }
  65. public function getPrice(){
  66. return ($this->price - $this->discount);
  67. }
  68. function getProducer(){
  69. return $this->producerFirstName." ".$this->producerMainName;
  70. }
  71. function getSummaryLine(){
  72. $base = "$this->title({$this->producerMainName},";
  73. $base .= "{$this->producerFirstName})";
  74. return $base;
  75. }
  76. }
  77. class CdProduct extends ShopProduct{
  78. private $playLength;
  79. function __construct($title,$firstName,$mainName,$price,$playLength){
  80. parent::__construct($title,$firstName,$mainName,$price);//继承父类的构造函数
  81. $this->playLength = $playLength;
  82. }
  83. function getPlayLength(){
  84. return $this->playLength;
  85. }
  86. function getSummaryLine(){
  87. $base = parent::getSummaryLine();
  88. $base .= ":playing time {$this->playLength}";
  89. return $base;
  90. }
  91. }
  92. class BookProduct extends ShopProduct{
  93. private $numPages = 0;
  94. function __construct($title,$firstName,$mainName,$price,$numPages){
  95. parent::__construct($title,$firstName,$mainName,$price);
  96. $this->numPages = $numPages;
  97. }
  98. function getnumPages(){
  99. return $this->numPages;
  100. }
  101. function getSummaryLine(){
  102. $base = parent::getSummaryLine();
  103. $base .= ":page count {$this->numPages}";
  104. return $base;
  105. }
  106. }
  107. $dsn = "sqlite:C:/Users/Administrator/Desktop/shop.db";
  108. $pdo = new PDO($dsn,null,null);
  109. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  110. $obj = ShopProduct::getInstance(1,$pdo);
  111. echo $obj->getSummaryLine();

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

php面向对象编程,php面向对象

php面向对象之克隆对象

php中对象的串行化,php对象串行化

以上就是PHP对象、模式与实践之高级特性案例分析的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行