php中extends是什么意思,在php中extends与implements的区别
PHP
類是單繼承,也就是不支持多繼承,當一個類需要多個類的功能時,繼承就無能為力了,為此 PHP 引入了類的接口技術。
接口的使用使用implements關鍵字,而對抽象類使用的是extends繼承關鍵字。
在接口中只能定義常量和方法,不能實現(xiàn)方法,const定義常量,functionUser();不能使用pubilc
$a ="a"與 pubilc static$a
="a";
//抽象類
abstract class Father {
function name() {
echo "name...
";
}
abstract function name2();
public $n="n1";
public static $n2="n2";
const n3="n3";
}
class Data extends Father {
function name2() {
echo "name2 of Data...
";
}
}
$v=new Data();
echo $v->var1."
";
echo Father::$n2."
";
echo Father::n3."
";
//實現(xiàn)接口
interface User{
function getDiscount();
function getUserType();
}
//VIP用戶 接口實現(xiàn)
class VipUser implements User{
private $discount = 0.5;
function getDiscount() {
return $this->discount;
}
function getUserType() {
return "VIP用戶";
}
}
class Goods{
//public $price="45";??????? 此處接口定義中不能包含成員變量
//public static $price="45"; 此處接口定義中不能包含靜態(tài)變量
//
const price = 45;
public $vc;
//定義 User 接口類型參數(shù),這時并不知道是什么用戶
function run(User $vc){
$this->vc = $vc;
$discount = $this->vc->getDiscount();
$usertype = $this->vc->getUserType();
echo $usertype."商品價格:".self::price*$discount;
}
}
$display = new Goods();
$display ->run(new VipUser);?//可以是更多其他用戶類型
?>
總結
以上是生活随笔為你收集整理的php中extends是什么意思,在php中extends与implements的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 不支持无符号整数,是否可以使用大
- 下一篇: 属于PHP语言结构的是,PHP语言结构