Static methods and attributes
We can access methods and attributes not only through objects, but also through classes. Such methods and attributes are "static" and must be declared with 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 are functions with classes as scopes. Static methods cannot access common attributes in this class, because those attributes belong to an object, but static attributes can be accessed. If a static property 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. To access static methods or attributes from the current class, use the self keyword. Self points to the current class, just as the pseudovariable $this points to the current object. By definition, we can't call static methods in objects, so static methods and attributes are also called class variables and attributes, so we can't use pseudovariables in static methods.
Next, build a static method of the ShopProduct class to automatically instantiate the ShopProduct object. Define table products using sqlite as follows:
CREATE TABLE products(
id INTEGER PRIMARY KEY AUTOINCREMENT,
type TEXT,
firstname TEXT,
mainname TEXT,
title TEXT,
price float,
numpages int,
playlength int,
discount int )
Next, build the getInstance () method, whose parameters are the id and pdo objects recorded in the database. Use them to get a row of records from the database, and then return to the ShopProduct object.
//ShopProduct class, see the previous blog "In-depth PHP Object-Oriented, Patterns and Practices - Objects"
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['numpages']
);
} 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);
Such a method, like a factory, can accept raw data (such as a list of data or configuration information) and then generate objects based on it.
PHP5 can define constant attributes in classes, which are declared by the const keyword. Constants begin with the dollar sign, unlike conventional attributes. By convention, constants can only be named in uppercase letters. Constant attributes can only contain basic data types and can only be accessed through classes.
class ShopProduct
{
const AVAILABLE = 0;
const OUT_OF_STOCK = 1;
//...
}
print ShopProduct::AVAILABLE;