PHP Object, pattern and practice: advanced feature analysis, php Object practice features

Source: Internet
Author: User

PHP Object, pattern and practice: advanced feature analysis, php Object practice features

This article describes the advanced features of PHP object-oriented programming. We will share this with you for your reference. The details are as follows:

Advanced features

Including:

1. Static methods and attributes (access data and functions through classes rather than objects)
2. abstract classes and interfaces (Design and Implementation separation)
3. Error Handling (exception)
4. Final classes and methods (restrict inheritance)
5. Interceptor (automatic delegation)
6. destructor (cleaning before object destruction)
7. Clone an object (create a copy of the object)
8. parse the object into a string

PS, learn to look at the code from the memory perspective. Imagine the world of computers.

Small examples of static methods

<?phpclass StaticExample{  static public $aNum = 10;  static public function sayHello(){    print "hello";  }}print StaticExample::$aNum."<br/>";StaticExample::sayHello();

Tips:

1. Static methods cannot be common attributes in the category, because those attributes belong to one object, but can access static attributes.
2. We can no longer call static methods in objects, so we can no longer use pseudo variable $ this 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;} 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 ['numpage']);} 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 ); // inherit the constructor $ 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 ();

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.