php static method

 Static method

(1) Static methods can not access the class attribute in common, since those attributes belonging to an object, but can access static properties;

(2) Access from the current class (not a subclass) static method or property, you can use the self keyword, self pointing to the current class, like $ this point to the same as the current object;

Calling (3) is no longer subject static methods, static methods and properties is also called class methods and class properties, and therefore can not use the pseudo object variable $ this.

Static method advantages:

(1) anywhere in the code can be used (assuming class can be accessed);

Each instance (2) class can access the static properties defined in the class, can be used to set the static property values, all objects of that class values ​​may be used;

(3) need not be able to access a static instance of the object property or method.

? <PHP 

$ = new new PDO the PDO ( 'MySQL: Host = localhost; dbname = mydb', 'the root'); // creates a PDO (PHP Data Object) the object 

class ShopProduct { 

    Private $ title; // property is also referred to as member variable, used to store different from each other between the object data 

    private $ producerMainName; // All the properties are set to private, does not allow direct external access these properties, and provide a way to obtain the desired value of 

    Private $ producerFirstName; 

    protected $ price; // stop outside direct access to the property can be accessed by subclasses 

    Private the Discount = $ 0; 

    Private $ the above mentioned id = 0; 

 

    // when an object is created, the constructor is called automatically, construction method can ensure the necessary property is set, and the need to do anything else 

    public function the __construct ($ title, $ mainName, firstName $, $. price) { 

        $ this-> title $ title =; // $ this pseudo-variable for each variable assignment 

        $ this-> producerMainName = mainName $; 

        $ this-> producerFirstName firstName = $;

        this- $>. price = $. price; 

    } 

 

    / ** 

     * @return Mixed This method returns only the property value called "access method", also referred to as a getter and the setter 

     * / 

    public getProducerFirstName function () 

    { 

        return $ this- > producerFirstName; 

    } 

 

    / ** 

     * @return Mixed access methods for surname 

     * / 

    public getProducerMainName function () 

    { 

        return $ this-> producerMainName; 

    } 

 

    / ** 

     * @param NUM discounted price $ 

     * / 

    public function setDiscount ($ NUM) 

    { 

        $ this-> = $ discount NUM; 

    } 

 

    / ** 

     * @return int discount access method 

     * /

    getDiscount function public () 

    { 

        return $ this-> Discount; 

    } 

 

    / ** 

     * @return mixed Book access method name or CD name 

     * / 

    public function the getTitle () 

    { 

        return $ this-> title; 

    } 

 

    / ** 

     * @ price after discount return mixed 

     * / 

    public function getPrice () 

    { 

        return ($ this->. price - $ this-> the discount); 

    } 

 

    / ** 

     * @return String author 

     * / 

    public function getProducer () {// method to let object to perform the task 

        .. return $ this-> producerMainName '' $ this-> producerFirstName; 

    } 

 

    / **

     * Summary invoice information @return string 

     * / 

    public getSummaryLine function () 

    { 

        $ Base = "{$ this-> title} ({$ this-> producerMainName}, {$ this-> producerFirstName})"; 

        return $ Base; 

    } 

 

    / ** 

     * @param $ ID ID of the access method, the setter 

     * / 

    public function the setId ($ ID) 

    { 

        $ this-> $ ID = ID; 

    } 

 

    // the data type of query data, a particular type of returned objects ShopProduct this method does not use any instance properties and methods, so it is defined as a static method (static), as long as there is a valid PDO object, you can call this method anywhere in the program. This method is the same as the factory, can accept raw data to produce specific types of objects. 

    static function getInstance public (the above mentioned id $, $ PDO PDO) 

    { 

        $ stmt = $ PDO -> PREPARE ( "the SELECT * the FROM Products's the WHERE the above mentioned id =?");

        $stmt -> execute(array($id));

        $row = $stmt -> fetch();

        if(empty($row)){

            return null;

        }

        switch($row['type'])

        {

            case 'book' :

                $product = new BookProduct($row['title'],$row['first_name'],$row['main_name'],$row['price'],$row['num_pages']);

            break;

            case 'cd' :

                $product = new CdProduct($row['title'],$row['first_name'],$row['main_name'],$row['price'],$row['play_length']);

            break;

            default :

                $product = new ShopProduct($row['title'],$row['first_name'],$row['main_name'],$row['price']);

            BREAK; 

        } 

        $ Product -> the setId ($ Row [ 'ID']); 

        $ Product -> setDiscount ($ Row [ 'Discount']); 

        return $ Product; 

    } 

} 

 

// if the derived class constructor is not defined, then it will be instantiated automatically call the parent class constructor. The default subclass inherits all the parent class public and protected method, but not private methods and properties inherited 

class BookProduct the extends ShopProduct 

{ 

    private $ numPages; 

 

    // each sub-class constructor call the parent class before setting their properties, the base class now know only their own data, the subclass generally special column of the parent class, you should avoid any news about parent child class. 

    function __construct public ($ title, $ mainName, $ firstName, $. price, $ numPages) 

    { 

        // parent keyword can be used in any replication method in the parent class method to expand the parent by the parent class method call in the current object class function, rather than to apply a class object's method, instead of using :: -> 

        parent :: __ Construct ($ title, $ mainName, firstName $, $. price);

        the this $ -> = $ numPages numPages; 

    } 

 

    / ** 

     * @param $ numPages pages of the book 

     * @return mixed Pages 

     * / 

    public getNumberOfPages function () 

    { 

        return $ this-> numPages; 

    } 

 

    / ** 

     * Sub class (derived class) may cover the modifications and parent (base class or superclass) functions 

     * @return string Book invoice summary information 

     * / 

    public getSummaryLine function () 

    { 

        $ :: base = parent getSummaryLine (); 

        $ base. = "Page COUNT - {$ this-> numPages}"; 

        return $ Base; 

    } 

} 

 

class CdProduct the extends ShopProduct 

{ 

    Private $ playLength;

 

    public function __construct($title,$mainName,$firstName,$price,$playLength)

    {

        parent::__construct($title,$mainName,$firstName,$price);

        $this -> playLength = $playLength;

    }

 

    /**

     * @return int 播放时间

     */

    public function getPlayLength()

    {

        return $this -> playLength;

    }

 

    /**

     * @return string CD发票的摘要信息

     */

    public function getSummaryLine()

    {

        $base = parent::getSummaryLine();

        $base .= "playing time - {$this->playLength}";

        return $base;

    }

}

 

$ product = ShopProduct :: getInstance (1 , $ pdo); // This static method id passed different types of objects to generate a specific 

IF ($ Product) { 

    Print "author: Product {$ -> getProducer ()} < / br> "; // author: Lun Xun 

    Print" Summary Line: Product {$ -> getSummaryLine ()} </ br> "; // Line Summary: Kong Yiji (Xun, Lun) Page COUNT - 200 is 

}

  

Guess you like

Origin www.cnblogs.com/68xi/p/11519022.html