This article illustrates the advanced features of object-oriented programming in PHP. Share to everyone for your reference, specific as follows:
Advanced Features
Including:
1. Static Methods and properties (access to data and functionality through classes rather than objects)
2. Abstract class and interface (design, implementation separation)
3. Error handling (Exception)
4.Final classes and methods (limit inheritance)
5. Interceptor (Automatic delegation)
6. destructor (cleanup before object destruction)
7. Cloning 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 micro-world of computers.
A small example of a static method
<?php
class staticexample{
static public $aNum = ten;
static public Function SayHello () {
print "Hello";
}
}
Print Staticexample:: $aNum. " <br/> ";
Staticexample::sayhello ();
Tips
1. Static methods cannot access normal properties in a class, because those properties belong to one object, but static properties can be accessed.
2. We cannot invoke static methods in objects, so we cannot use pseudo variable $this in static methods.
A large example of a 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;
The Public Function SetID ($id) {$this->id = $id;
The public static function getinstance ($id, PDO $pdo) {$query = "SELECT * from the 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 [' Mai Nname '], $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;
The Public Function Getproducerfirstname () {return $this->producerfirstname;
The Public Function Getproducermainname () {return $this->producermainname;
The Public Function SetDiscount ($num) {$this->discount = $num;
The Public Function Getdiscount () {return $this->discount;
The Public Function GetTitle () {return $this->title;
The Public Function GetPrice () {return ($this->price-$this->discount); function Getproducer () {return $this->producerfirstname. "
". $this->producermainname;
The 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)//Inheriting the constructor of the parent class $this->playlength = $playLength;
function Getplaylength () {return $this->playlength;
function Getsummaryline () {$base = Parent::getsummaryline ();
$base. = ":p laying 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. = ":p Age 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 ();
More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design.