您好,欢迎访问一九零五行业门户网

php中静态方法的属性有哪些

php中静态方法的属性:
(1)静态方法不能访问这个类中的普通属性,因为那些属性属于一个对象,但可以访问静态属性;
(2)从当前类(不是子类)中访问静态方法或属性,可以使用 self 关键字,self 指向当前类,就像 $this  指向当前对象一样;
(3)不能再对象中调用静态方法,静态方法和属性又被称为类方法和类属性,因此不能再对象中使用伪变量 $this 。
静态方法的优点:
(1)在代码的任何地方都可以用(假设可以访问该类);
(2)类的每个实例都可以访问类中定义的静态属性,可以利用静态属性来设置值,该值可以被类的所有对象使用;
(3)不需要实例对象就能访问静态属性或方法。
<?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}
推荐教程: 《php教程》
以上就是php中静态方法的属性有哪些的详细内容。
其它类似信息

推荐信息