The examples in this article describe the advanced features of PHP object-oriented programming. Share it with everyone for your reference, as follows:
Advanced features
include:
1. Static methods and properties (access data and functions through classes instead of objects)
2. Abstract classes and interfaces (separation of design and implementation )
3. Error handling (exceptions)
4. Final classes and methods (restricted inheritance)
5. Interceptors (automatic delegation)
6. Destructor methods (cleanup before object destruction)
7. Clone objects (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 access ordinary 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();
I hope this article will be helpful to everyone in PHP programming. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!