function name" method call."/> function name" method call.">
Home >Backend Development >PHP Problem >How to use static methods in php
In PHP, member methods modified by the static keyword are called static methods. To call a static method, you can use the "class name::static method ()" syntax, or you can directly instantiate a variable, and then Use the method call of "Variable name->Function name".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, modified by the static keyword Member properties and member methods are called static properties and static methods. Here they can be collectively referred to as static members. Static members in a class are different from general members in the class. Static members will not be instantiated into objects, that is to say We can access static members through a class without instantiating a class.
It is easy to declare static variables in a class. We can add a static keyword before an ordinary member of the class to turn this ordinary member into a static member. In this way, we can directly access these static members in the class without instantiating the class. The syntax format for accessing static members is as follows:
类名::$静态属性 类名::静态方法()
The ::
symbol is called the range resolution operator, which is used to access static members, static methods and constants, and can also be used to override classes members and methods in .
Or directly instantiate a variable and then call it using the method "Variable name->Function name
".
Since static methods can be called without instantiation of the class, and ordinary methods and ordinary properties must first declare an object before they can be accessed, therefore, ordinary methods and access cannot always be used in static methods. Ordinary variables.
One thing to remember about static methods: "Static methods belong to the entire class."
class Star{ public static function getName($name){ return '队员姓名'.$name; } } echo Star::getName('kate'); //队员姓名kate $star = new Star(); echo $star->getName('jim'); //队员姓名jim
Advantages of static methods:
(1) Can be used anywhere in the code (assuming the class can be accessed);
(2) Each instance of the class can access the static properties defined in the class, and can use static properties to set values , this value can be used by all objects of the class;
(3) Static properties or methods can be accessed without instance objects.
<?php $pdo = new PDO('mysql:host=localhost;dbname=mydb','root'); // 生成一个 PDO(PHP Data Object) 对象 class ShopProduct{ private $title; // 属性也称为成员变量,用来存放对象之间互不相同的数据 private $producerMainName; // 所有的属性都设置为 private,不允许外部直接访问这些属性,然后提供方法来取得需要的值 private $producerFirstName; protected $price; // 阻止外面直接访问该属性,可以被子类访问 private $discount = 0; private $id = 0; // 创建对象时,构造方法会被自动调用,构造方法可以确保必要的属性设置,并完成任何需要准备的工作 public function __construct($title,$mainName,$firstName,$price){ $this->title = $title; // 使用伪变量$this给每个变量赋值 $this->producerMainName = $mainName; $this->producerFirstName = $firstName; $this->price = $price; } /** * @return mixed 这种只返回属性值的方法称为“访问方法”,也称为 getter 和 setter */ public function getProducerFirstName() { return $this->producerFirstName; } /** * @return mixed 获取 姓 的访问方法 */ public function getProducerMainName() { return $this->producerMainName; } /** * @param $num 打折的价格 */ public function setDiscount($num) { $this->discount = $num; } /** * @return int 折扣的访问方法 */ public function getDiscount() { return $this->discount; } /** * @return mixed Book名或CD名的访问方法 */ public function getTitle() { return $this->title; } /** * @return mixed 折扣后的价格 */ public function getPrice() { return ($this->price - $this->discount); } /** * @return string 作者 */ public function getProducer(){ // 方法让对象执行任务 return $this->producerMainName .' '. $this->producerFirstName; } /** * @return string 发票的摘要信息 */ public function getSummaryLine() { $base = "{$this->title} ( {$this->producerMainName},{$this->producerFirstName}) "; return $base; } /** * @param $id id 的访问方法, setter */ public function setId($id) { $this->id = $id; } // 根据查询数据的数据类型,返回特定类型的 ShopProduct 对象,这个方法没有使用任何实例的属性和方法,因此把它定义为一个静态方法(static),只要有一个有效的 PDO 对象,就可以在程序的任何地方调用这个方法。这个方法就像工厂一样,可以接受原始数据产生特定类型的对象。 public static function getInstance($id,PDO $pdo) { $stmt = $pdo -> prepare("SELECT * FROM products WHERE 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 -> setId($row['id']); $product -> setDiscount($row['discount']); return $product; } } // 若派生类没有定义构造方法,则它在实例化是会自动调用父类的构造方法。子类默认继承父类所有的 public 和 protected 方法,但不继承 private 方法和属性 class BookProduct extends ShopProduct { private $numPages; // 每个子类在设置自己的属性前调用父类的构造方法,基类现在仅知道自己的数据,子类一般是父类的特列,应该避免告诉父类任何关于子类的消息。 public function __construct($title,$mainName,$firstName,$price,$numPages) { // parent 关键字可以在任何复写父类方法的方法中使用,通过在当前对象中调用父类的方法来拓展父类的功能 , 要应用一个类而不是对象的方法,使用 :: 而不是 -> parent::__construct($title,$mainName,$firstName,$price); $this -> numPages = $numPages; } /** * @param $numPages 书的页数 * @return mixed 总页数 */ public function getNumberOfPages() { return $this->numPages; } /** * 子类(派生类)可以覆盖和修改父类(基类或超类)的功能 * @return string Book发票的摘要信息 */ public function getSummaryLine() { $base = parent::getSummaryLine(); $base .= "page count - {$this->numPages}"; return $base; } } class CdProduct 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); // 该静态方法根据传入 id 的不同生成特定类型的对象 if($product){ print "author : {$product -> getProducer()} </br>"; // author :Lun Xun print "summary line: {$product -> getSummaryLine()}</br>"; // summary line: Kong Yiji ( Xun,Lun) page count - 200 }
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to use static methods in php. For more information, please follow other related articles on the PHP Chinese website!