In-depth understanding of PHP object-oriented, models and practices-Advanced features (1)

Source: Internet
Author: User
We can not only access them through object access methods and attributes, but also through classes. Such methods and attributes are "static" and must be declared using the static keyword.

Static methods and attributes

We can not only access them through object access methods and attributes, but also through classes. Such methods and attributes are "static" and must be declared using the static keyword.

class StaticExample{    static public $aNum=0;    static public function sayHello()    {        self::$aNum++;        print "hello (".self::$aNum.")/n";    }}print StaticExample::$aNum;StaticExample::sayHello();

Static methods use classes as the function scope. Static methods cannot access common attributes in this class, 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. When accessing static elements, use: to connect class names and attributes or class names and methods. You can use the self keyword to access static methods or attributes from the current class. Self points to the current class, just as the pseudo variable $ this points to the current object. According to the definition, we cannot call static methods in objects. Therefore, static methods and attributes are also called class variables and attributes. Therefore, pseudo variables cannot be used in static methods.

Next we will build a static method of the ShopProduct class to automatically instantiate the ShopProduct object. Use sqlite to define the table products, as shown below:

CREATE TABLE products(    id INTEGER PRIMARY KEY AUTOINCREMENT,    type TEXT,    firstname TEXT,    mainname TEXT,    title TEXT,    price float,    numpages int,    playlength int,    discount int )

The getInstance () method is constructed below. its parameters are the id and pdo object of the database record. Use them to obtain a row of records of the database, and then return the ShopProduct object.

// ShopProduct class, private $ id = 0 ;//... public function setID ($ id) {$ this-> $ id ;}//... public static function getInstance ($ id, PDO $ pdo) {$ stmt = $ pdo-> prepare ("select * from products where id =? "); $ Result = $ stmt-> execute (array ($ id); $ 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']); $ producerMainName-> setDiscount ($ row ['discount']); return $ product ;}//...

As long as there is a valid PDO object, we can call this method anywhere in the program:

$dsn = "sqlite://home/bob/projects/products.db";$pdo = new PDO($dsn, null, null);$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);$obj = ShopProduct::getInstance(1, $pdo);

Like a factory, this method can accept raw data (such as a column of data or configuration information) and then generate an object based on this.

PHP5 can define constant attributes in the class, and the constant attributes are declared using the const keyword. Constants do not start with a dollar sign as common properties do. By convention, constants can only be named with uppercase letters. Constant attributes can only contain basic data types and can only access constant attributes through classes.

class ShopProduct{    const AVAILABLE    = 0;    const OUT_OF_STOCK = 1;    //...}print ShopProduct::AVAILABLE;

The above is a detailed description of PHP object-oriented, model and practice-Advanced features (1). For more information, see other related articles in the first PHP community!

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.