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

php怎么使用静态方法

在php中,通过static关键字修饰的成员方法被称为静态方法,调用静态方法可以使用“类名::静态方法()”语法,也可以直接实例化一个变量,然后使用“变量名->函数名”的方法调用。
本教程操作环境:windows7系统、php7.1版,dell g3电脑
在 php 中,通过 static 关键字修饰的成员属性和成员方法被称为静态属性和静态方法,这里可以将它们统称为静态成员,类中的静态成员与类中的一般成员不同,静态成员不会被实例化到对象中,也就是说我们不需要实例化一个类,就可以通过类来访问静态成员。
在类中声明静态变量很容易,我们可以在类的普通成员前加一个 static 关键字,就可以将这个普通成员变成静态成员了。这样一来,我们可以在不实例化类的情况下,直接访问类中的这些静态成员。访问静态成员的语法格式如下:
类名::$静态属性类名::静态方法()
其中::符号被称为范围解析操作符,用于访问静态成员、静态方法和常量,还可以用于覆盖类中的成员和方法。
或者直接实例化一个变量,然后使用“变量名->函数名”的方法调用。
由于静态方法不需要类的实例化就能够被调用,而普通方法和普通属性都必须先声明一个对象才可以访问,因此,在静态方法里面始终是不能使用普通方法和访问普通变量的。
关于静态方法要牢牢记住一点:“静态方法是属于整个类的。”
class star{public static function getname($name){return '队员姓名'.$name;}}echo star::getname('kate'); //队员姓名kate$star = new star();echo $star->getname('jim'); //队员姓名jim
静态方法的优点:
(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怎么使用静态方法的详细内容。
其它类似信息

推荐信息