一路走來也見識了幾種語言的面向?qū)ο螅罟殴值氖莏avascript的,但是發(fā)現(xiàn)都沒有深入,現(xiàn)在想系統(tǒng)的記錄下php面向?qū)ο蟮膶W(xué)習(xí)

image.png
類名的每個單詞首字母大寫
類可以通過extends繼承另一個類,不支持多繼承,可以在子類中重寫父類中的屬性和方法,方法重寫參數(shù)必須保持一致
可以通過 對象名->屬性或$this->屬性來調(diào)用非靜態(tài)屬性
對象名::屬性或$self::屬性來調(diào)用靜態(tài)屬性
<?php
class Person{
public $food = "apple"; //屬性
public function eat(){
echo "i like eat ".$this->food;
}
}
$doctor = new person();
$doctor->eat();
?>
php是單繼承
static 在兩個對象之間共享相同的屬性,修改一個,另一個也相應(yīng)修改
public static $name = 'weiwei';
在類中使用靜態(tài)成員,使用self::靜態(tài)成員來調(diào)用,在類的外部使用靜態(tài)成員,使用類名::靜態(tài)成員來調(diào)用,使用self可以訪問類自身的成員方法,訪問常量可以不加$符號
使用parent關(guān)鍵字可以訪問父類的靜態(tài)成員,還可以訪問父類中被子類重寫的方法
靜態(tài)方法只能訪問靜態(tài)屬性
不能用$this->訪問靜態(tài)屬性,方法
靜態(tài)方法內(nèi)部不能使用$this
(1)隨機給定長度給定類型字符串類,類型有數(shù)字,字母,字母加數(shù)字
class RandStr{
public $length;
public $para_type;
public function __construct($length=7,$para_type=2){
$this->length = $length;
$this->para_type = $para_type;
}
public function generateRandStr(){
switch ($this->para_type) {
case 1:
$string = join('',array_rand(array_flip(range(0,9)),$this->length));
echo $string;
break;
case 2;
$string = join('',array_rand(array_flip(range('a','z')),$this->length));
echo $string;
break;
default:
$string = join('',array_rand(array_flip(array_merge(range(0,9),range('a','z'))),$this->length));
echo $string;
break;
}
}
}
$weidapao = new RandStr(5,3);
$weidapao->generateRandStr();
(2)parent的使用
class house{
private $wallcolor;
private $window_num;
public function __construct($wallcolor = 'white',$window_num = 2){
$this->wallcolor = $wallcolor;
$this->window_num = $window_num;
}
public function house_info(){
$info = "房屋的基本信息有:"."墻壁顏色:".$this->wallcolor."<br>窗戶數(shù)量:".$this->window_num;
return $info;
}
}
class library extends house{
private $rect_size;
public function __construct($rect_size = 400){
parent::__construct();
$this->rect_size = $rect_size;
}
public function library_info(){
$info = parent::house_info();
echo $info."占地面積為:".$this->rect_size;
}
}
//$myhouse = new house('blue',3);
//$myhouse->house_info();
$library = new library(500);
$library->library_info(); //繼承的方法可以和子類的方法名不同
(3)abstract抽象類
- 當(dāng)該類至少有一個抽象方法,該類為抽象類
- 抽象類不能被實例化,只是定義了調(diào)用方式,沒有具體實現(xiàn)
- 子類繼承的時候必須實現(xiàn)所有的抽象方法
- 子類在實現(xiàn)父類方法的時候,方法可見性可以與父類相同或者寬松
- 為子類提供一種規(guī)范
(圖形面積周長的計算)
<?php
header("content-type:text/html;charset=utf-8");
//當(dāng)運行顯示文件下載,可能是text/html順序錯誤
/*
定義抽象類Shape
protected 屬性 $width $height
protected 方法 getArea() getPrimeter()
矩形 Rectangle 繼承 Shape
圓形 Circle 繼承 Shape
*/
abstract class Shape{
//有抽象方法就必須聲明為抽象類
protected $width;
protected $height;
public function __construct($width,$height){
$this->width = $width;
$this->height = $height;
}
abstract protected function getArea();
abstract protected function getPrimeter();
//抽象方法不加大括號{},抽象方法后要加分號
}
class Rectangle extends Shape{
public function getArea(){
echo "矩形面積為:".($this->width * $this->height);
}
public function getPrimeter(){
echo "矩形周長為:".(($this->width) * 2 + ($this->height) * 2)."<br>";
}
//涉及到計算部分的字符串連接,要對計算部分加括號
}
class Circle extends Shape{
private $radius;
public function __construct($radius){
$this->radius = $radius;
}
//因為涉及到傳參數(shù)量不同,因此重寫構(gòu)造函數(shù)
public function getArea(){
echo "圓形面積為:".(3.14 * $this->radius * $this->radius)."<br>";
}
public function getPrimeter(){
echo "圓形周長為:".(3.14 * $this->radius *2)."<br>";
}
}
$playground = new Rectangle(20,30);
$playground->getArea();
$playground->getPrimeter();
$circle = new Circle(5);
$circle->getArea();
$circle->getPrimeter();
抽象類只能單繼承,接口可以多繼承,也可以多實現(xiàn),接口里的成員字段必須是常量,而抽象類可以共享非抽象方法,也可以有其他非抽象屬性字段
(4)interface接口
<?php
header("content-type:text/html;charset=utf-8");
interface english{
public function eng_score();
}
interface history{
public function his_score();
}
class Student implements history,english{
public function eng_score(){
echo "我的英語得了99分<br>";
}
public function his_score(){
echo "我的歷史得了88分";
}
}
$weidapao = new Student;
$weidapao->eng_score();
$weidapao->his_score();
- 類可以實現(xiàn)多個接口
- 接口繼承多個接口,類必須實現(xiàn)所有實現(xiàn)接口的方法
<?php
header("content-type:text/html;charset=utf-8");
interface farther{
public function far_info();
}
interface mother{
public function mom_info();
}
interface familyMember extends farther,mother{
public function all_info();
}
class Son implements familyMember{
public function all_info(){
echo "我們是幸福的一家人<br>";
}
public function far_info(){
echo "我是偉大炮的爸爸<br>";
}
public function mom_info(){
echo "我是偉大炮的媽媽<br>";
}
}
$weiwei = new Son; //如果不傳參,可以不加括號
$weiwei->all_info();
$weiwei->far_info();
$weiwei->mom_info();
(5)static靜態(tài)
<?php
header("content-type:text/html;charset=utf-8");
class Plane{
static public $boom = 0;
static private $engine = 2;
static public function plane_info(){
echo "<br>炸彈的數(shù)量為:".self::$boom."<br>發(fā)動機的數(shù)量為:".self::$engine; //使用self::屬性名 屬性名需要加$
}
public function no_static_plane_info(){
echo "<br>非靜態(tài)方法調(diào)用靜態(tài)屬性,發(fā)動機的數(shù)量為:".self::$engine;
}
}
$f4 = new Plane;
echo "類外調(diào)用,炸彈的數(shù)量為:".Plane::$boom;
$f4->plane_info();
$f4->no_static_plane_info(); //非靜態(tài)方法可以調(diào)用靜態(tài)屬性
(6)多態(tài)
<?php
header("content-type:text/html;charset=utf-8");
interface Sports{
public function sport_type();
public function sport_way();
}
class BasketBall implements Sports{
public function sport_type(){
echo "運動類型是打籃球<br>";
}
public function sport_way(){
echo "打籃球是五人對五人,一個球<br>";
}
}
class FootBall implements Sports{
public function sport_type(){
echo "運動類型是踢足球<br>";
}
public function sport_way(){
echo "踢足球是十一人對十一人,一個球,兩個守門員<br>";
}
}
class Person{
public function whichSport($type,$way){
$type."<br>".$way;
}
}
$basketball = new BasketBall;
$football = new FootBall;
$weidapao = new Person;
//根據(jù)傳入對象不同,不同對象調(diào)用相同方法產(chǎn)生不同的結(jié)果
$weidapao->whichSport($basketball->sport_type(),$football->sport_way());