Static method and attribute directory Table Of Contents
- Why do I need to use static methods and attributes?
Static methods and attributes
Static methods and attributes are actually accessing data and functions through classes rather than through objects.
The actual operations in object-oriented programming are completed through the instance of the class (rather than the class itself). after all, the class is only the template of the instance. But we can use static to define the methods and attributes of the class. this is equivalent to static, without dynamic instantiation class, but instead of static direct calls.
Static methods are class-oriented functions. static methods cannot access common attributes in this class because these attributes belong to an object, but can access static attributes.
Because static elements are accessed through classes rather than instances, you do not need to reference object variables when accessing static elements, but use double colons: to access them.
StaticExample::sayHello();
A subclass can use the parent keyword to access the parent class without its class name. To access static methods and attributes from a class (not a subclass), you can use the self keyword. self points to the current class, just as $ this points to the current object.
Why do I need to use static methods and attributes?
1. because they are available in all tasks in the code, that is to say, you do not need to transfer class instances between objects or store instances in global variables, you can use the methods and attributes of the category class.
2. each instance of the class can have the static attribute defined in the category class, so you can use the static attribute to set the value, which can be used by all the objects of Thunder.
3. you can access static attributes and methods without instance objects, so that you do not need to instantiate objects to obtain a simple function.
<? Phpclass ShopProduct {private $ id = 0; public function setID ($ id) {$ this-> id = $ id;} public static function getInstance ($ id, PDO $ pdo) {// The key here is that the getinstance method is a static method and two parameters are passed in, one is id and the other is pdo instance $ stmt = $ pdo-> prepare ("select * from products where id =? "); $ Result = $ stmt-> execute (arrary ($ id); $ product = new BookProduct ($ result ['title']); // A shopproduct object of a specific type is set here. this is a shopproduct object of the book $ product-> setID ($ result ['id']); return $ product; // This method does not use any instance attributes and methods. as long as there is a valid pdo object, we can call it anywhere in the program, and the returned Shopproduct object is of a specific type }}$ 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 it directly without instantiating the object. In addition, the pdo object can be changed at any time without being bound to a pdo object.?>
Like a factory, this method can accept raw data and then generate objects based on the data.