About static methods and properties

Source: Internet
Author: User
Tags dsn stmt
Catalog Table of Contents

    • Why use static methods and properties?

Static Methods and properties

Static methods and properties, in fact, are access to data and functionality through classes rather than objects.

The actual operation in object-oriented programming is done through an instance of the class (not the class itself), after all, the class is just the template for the instance. But we can use static to access the methods and properties of the class, which is equivalent to static, no dynamic instantiation of the class, but a static direct call.

Static methods are functions that are scoped to a class, and static methods cannot access the normal properties in this class because they belong to an object but can access static properties.

Because static elements are accessed through classes rather than instances, accessing static elements does not require referencing the variables of the object, but instead uses double colons:: To access.

Staticexample::sayhello ();

A subclass can use the parent keyword to access the parental class without using its class name. To access static methods and properties from a class (not a subclass), you can use the Self keyword, and self points to the current class, just as $this points to the current object.

Why use static methods and properties?

1. You can access the methods and properties of a class because they are available in the code, that is, you do not need to pass an instance of the class between objects, and you do not need to tell the instance to be stored in a global variable.

2. Each instance of the class can access the static properties defined in the class, so you can use a static property to set the value that can be used by all the objects of the Thunder.

3. Static properties and methods can be accessed without the need for an instance object, so that we can instantiate the object without having to get a simple function.

< Phpclass shopproduct{    private $id = 0;    Public Function SetID ($id) {        $this->id = $id;    }    public static function getinstance ($id, PDO $pdo) {  //key here, the GetInstance method is a static method, and the incoming is 2 parameters, one is the ID one is the instance of PDO        $ stmt = $pdo->prepare ("SELECT * FROM Products where id=?");        $result = $stmt->execute (arrary ($id));        $product = new Bookproduct ($result [' title ']);  Here specifically set a specific type of shopproduct object, which is a book of the Shopproduct object        $product->setid ($result [' id ']);        return $product; This method does not use any instances of the properties and methods, as long as there is a valid PDO object, we can call it anywhere in the program, and he returned a specific type of Shopproduct object    }} $dsn = "sqlite://xxxxxx/ Products.db "; $pdo = new PDO ($DSN, null,null); $pdo->setattribute (pdo::attr_errmode,pdo::errmote_exception); $obj = Shopproduct::getinstance (1, $pdo); Here, we call directly, without instantiating the object. Also, the PDO object can be replaced at any time without having to constrain it to a PDO object.?>

Such a method, like a factory, can accept raw data and then produce objects based on that data.

  • Related Article

    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.