This article mainly introduces the advanced features of PHP objects, patterns and practices, and analyzes the concepts and simplicity of static properties and methods, abstract classes, interfaces, interceptors, cloned objects, etc. in PHP object-oriented programming in the form of examples. For the implementation method, friends who need it can refer to
. The details are as follows:
Advanced features
include:
1. Static method and attributes (access data and functionality through classes rather than objects)
2. Abstract classes and interfaces (design, implementation separation)
3. Error handling (exceptions)
4. Final classes and methods (restrictions Inheritance)
5. Interceptor (automatic delegation)
6. Destructor method (cleanup before object destruction)
7. Clone object (create a copy of the object)
8. Parse the object Into a string
PS, learn to look at the code from the perspective of memory. Imagine the microscopic world of computers.
Small example of static method
<?php class StaticExample{ static public $aNum = 10; static public function sayHello(){ print "hello"; } } print StaticExample::$aNum."<br/>"; StaticExample::sayHello();
tips:
1. Static methods cannot be accessed Normal properties in a class, because those properties belong to an object, but static properties can be accessed.
2. We cannot call static methods in objects, so we cannot use the pseudo variable $this in static methods.
Big example of static method
<?php class ShopProduct{ private $title; private $producerMainName; private $producerFirstName; protected $price; private $discount = 0; private $id = 0; function __construct($title,$firstName,$mainName,$price){ $this->title = $title; $this->producerFirstName = $firstName; $this->producerMainName = $mainName; $this->price = $price; } public function setID($id){ $this->id = $id; } public static function getInstance($id,PDO $pdo){ $query = "select * from products where id= '$id'"; $stmt = $pdo->query($query); $row = $stmt->fetch(); if(empty($row)){ return null; } if($row['type'] == "book"){ $product = new BookProduct($row['title'], $row['firstname'], $row['mainname'], $row['price'], $row['numpages'] ); }else if($row['type'] == "cd"){ $product = new CdProduct($row['title'], $row['firstname'], $row['mainname'], $row['price'], $row['playLength'] ); }else{ $product = new ShopProduct($row['title'], $row['firstname'], $row['mainname'], $row['price'] ); } $product->setId($row['id']); $product->setDiscount($row['discount']); return $product; } public function getProducerFirstName(){ return $this->producerFirstName; } public function getProducerMainName(){ return $this->producerMainName; } 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); } function getProducer(){ return $this->producerFirstName." ".$this->producerMainName; } function getSummaryLine(){ $base = "$this->title({$this->producerMainName},"; $base .= "{$this->producerFirstName})"; return $base; } } class CdProduct extends ShopProduct{ private $playLength; function __construct($title,$firstName,$mainName,$price,$playLength){ parent::__construct($title,$firstName,$mainName,$price);//继承父类的构造函数 $this->playLength = $playLength; } function getPlayLength(){ return $this->playLength; } function getSummaryLine(){ $base = parent::getSummaryLine(); $base .= ":playing time {$this->playLength}"; return $base; } } class BookProduct extends ShopProduct{ private $numPages = 0; function __construct($title,$firstName,$mainName,$price,$numPages){ parent::__construct($title,$firstName,$mainName,$price); $this->numPages = $numPages; } function getnumPages(){ return $this->numPages; } function getSummaryLine(){ $base = parent::getSummaryLine(); $base .= ":page count {$this->numPages}"; return $base; } } $dsn = "sqlite:C:/Users/Administrator/Desktop/shop.db"; $pdo = new PDO($dsn,null,null); $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $obj = ShopProduct::getInstance(1,$pdo); echo $obj->getSummaryLine();
php object-oriented programming, php object-oriented
php object-oriented clone object
Serialization of objects in php, php object serialization
The above is the detailed content of Case analysis of advanced features of PHP objects, patterns and practices. For more information, please follow other related articles on the PHP Chinese website!