Home>Article>Backend Development> What are the attributes of static methods in php

What are the attributes of static methods in php

hzc
hzc Original
2020-06-10 14:18:17 2832browse

What are the attributes of static methods in php

Attributes of static methods in php:

(1) Static methods cannot access ordinary attributes in this class, because those attributes belong to a Object, but you can access static properties;

(2) To access static methods or properties from the current class (not a subclass), you can use the self keyword, self points to the current class, just like $this points to the current object Same;

(3) Static methods cannot be called in objects. Static methods and attributes are also called class methods and class attributes, so the pseudo variable $this cannot be used in objects.

Advantages of static methods:

(1) Can be used anywhere in the code (assuming the class can be accessed);

(2) Every instance of the class Static properties defined in the class can be accessed, and static properties can be used to set values, which can be used by all objects of the class;

(3) Static properties or methods can be accessed without instance objects.

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()} 
"; // author :Lun Xun print "summary line: {$product -> getSummaryLine()}
"; // summary line: Kong Yiji ( Xun,Lun) page count - 200 }

Recommended tutorial: "php tutorial"

The above is the detailed content of What are the attributes of static methods in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn