A case study of the advanced features of PHP objects, patterns, and practices

Source: Internet
Author: User
This article mainly introduces PHP objects, patterns and practices of advanced features, combined with examples of PHP object-oriented programming in the static properties and methods, abstract classes, interfaces, interceptors, cloning objects and other concepts and simple implementation methods, the need for friends can refer to the next

Specific as follows:

Advanced Features

Including:

1. Static Methods and properties (access to data and functionality through classes, not objects)
2. Abstract classes and interfaces (design, implementation separation)
3. Error handling (Exception)
4.Final classes and methods (restricting inheritance)
5. Interceptors (auto-commissioned)
6. destructor (cleanup work before object destruction)
7. Cloning an object (creating a copy of the object)
8. Parse the object into a string

PS, learn the code from the memory point of view. Imagine the microscopic world of computers.

A small example of a static method

<?phpclass 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 the class, because those properties belong to an object, but static properties can be accessed.
2. We can no longer invoke static methods in objects, so pseudo-variable $this can no longer be used in static methods.

Large examples of static methods

<?phpclass 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;    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 [' Mai    Nname '], $row [' Price '], $row [' numpages ']); }else if ($row [' type '] = = "CD") {$product = new cdproduct ($row [' title '], $row [' FirstName '], $row [' Mainna Me '], $row [' Price '], $row [' Playlength ']        ); }else{$product = new Shopproduct ($row [' title '], $row [' FirstName '], $row [' Mainname '], $row [' Pric    E ']);    } $product->setid ($row [' id ']);    $product->setdiscount ($row [' Discount ']);  return $product;  } public Function Getproducerfirstname () {return $this->producerfirstname;  } public Function Getproducermainname () {return $this->producermainname;  The 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);//Inherit the parent class's constructor $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 (); 

The above is the whole content of this article, I hope that everyone's study has helped.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.