PHP Object-Oriented Learning 2: an in-depth understanding of advanced object-oriented features

Source: Internet
Author: User
PHP Object-Oriented Learning 2: in-depth understanding of object-oriented advanced features static methods and attributes: accessing data and functions through classes rather than objects static methods are functions acting on by classes, you cannot access common attributes in this class, because those attributes belong to one object, but can access static attributes. If you modify a static attribute, PHP Object-Oriented Learning 2: an in-depth understanding of advanced object-oriented features
Static methods and attributes: access data and functions through classes rather than objects. static methods use classes as functions and cannot access common attributes in these classes, because those attributes belong to one object, but can access static attributes. If a static attribute is modified, all instances of the class can access the new value. For example:
print staticExample::$aNum;StaticExample::sayHello();

Key point: unless you access an overwriting method, you can only use: access methods and attributes explicitly declared as static. ①: Static methods cannot be called in objects ②: pseudo variables cannot be used in static methods $ this
 Id = $ id;} public static function getInstance ($ id, PDO $ pdo) {$ stmt = $ pdo-> prepare ("select * from products where id =? "); $ Result = $ stmt-> execute (array ($ id); $ row = $ stmt-> fetch (); // instantiate the CD class $ product = new CDProudct ($ row ['title'], $ row ['firstname'], $ row ['mainname'], $ row ['price'], $ row ['playlength']); $ product-> setId ($ row ['id']); $ product-> setDiscount ($ row ['dusciybt ']); return $ product;}/** methods are a bit like factories, the raw data or configuration can be used to generate the object */$ dsn = 'sqlite: // home/db/bob/projects/products. db'; $ pdo = new PDO ($ dsn, null, null); $ pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); $ obj = shopProduct :: getInstance (1, $ pdo );

Abstract class and interface, subclass can inherit from it and implement the abstract method to make the abstract class concrete. An abstract class must contain at least one abstract method.
abstract class shopProductWriter{    protected $products=array();    abstract public function write();}

 Products [] = $ shopProduct;} abstract public function write ();}/*** output XML */class xmlProductWriter extends shopProductWriter {public function write () {$ str ='
 '. "\ N"; $ str. ="
 
  
\ N "; foreach ($ this-> products as $ shopProduct) {$ str. =" \ t
  
   
GetTitle (). "\"> \ n "; //...} $ str. ="
  
 \ N ";}}


Abstract classes provide specific implementation standards, while interfaces are pure templates. The interface can only define functions, but does not include implemented content interfaces. the interface can contain attributes and method declarations, but the method is empty. for example:
interface Chargeable{  public function getPrice();}

class shopProduct implements Chargeable{  //...  public function getPrice(){    return ;//...  }}

Interceptor method: automatically delegates PHP to provide the built-in interceptor method to intercept messages sent to undefined distributions and attributes. _ Get ($ property) is called when an undefined property is accessed. _ set ($ property, $ value) is called when an undefined property is assigned a value. _ isset ($ property) _ unset ($ property) is called when isset () is called for unset (). _ call ($ method, $ arg_array) is called when unset ($ property) is called for unset) called when an undefined method is called
 $ Method () ;}} function getName () {return "Bob" ;}function getAge () {return 24 ;}$ p = new Person (); print $ p-> name;
Destructor: the cleanup before the object is destroyed is automatically called before the object is collected by the garbage collector (that is, before the object is deleted from the memory.
 Name = $ name; $ this-> age = $ age;} function setID ($ id) {$ this-> id = $ id;} function _ destruct () {if (! Empty ($ this-> id) {// save the Person data print "saving person \ n" ;}}$ person = new Person ("bob", 24 ); $ person-> setID (111); unset ($ person); // output // Save Person
Callback: add functions for components using anonymous functions

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.