
批改狀態(tài):合格
老師批語:
根據(jù)prs-4規(guī)范,類的名稱與類的文件名要保持一直,所以定義一個類的名稱及文件名時要注意保持相同,否則自動加載會出現(xiàn)問題。
創(chuàng)建一個Player類,此類我們用作為父類
<?php //定義命名空間 namespace controller; //定義一個類 class Player{ // 成員屬性前一定要有訪問修飾符 public protected private public $name; public $height; public $team; protected $playNum = 23;// protected 只能被本類和子類訪問 private $weight;// 只能被本類訪問 //__construct()魔術(shù)方法--構(gòu)造方法:構(gòu)造函數(shù)(構(gòu)造器)除了可以給public公有成員初始化賦值外,還可以用來給私有成員,受保護(hù)的成員初始化賦值 public function __construct($name,$height,$team,$playNum,$weight){ //對象成員之間的內(nèi)部訪問可以用墻頭草$this $this->name = $name; $this->height = $height; $this->team = $team; $this->playNum = $playNum; $this->weight = $weight; } //聲明兩個方法 public function jog() { return "$this->name is jogging,whose playNum is $this->playNum<br>"; } public function shoot() { return "$this->name is shooting,weighing $this->weight<br>"; } } ?>
創(chuàng)建一個CnPlayer子類,子類繼承父類Player的所有的內(nèi)容,子類中可以擴(kuò)展新增屬性跟方法,也可以重寫父類的方法:
class CnPlayer extends Player { //新增一個屬性 public $country; //給子類創(chuàng)建構(gòu)造函數(shù)(重寫父類的構(gòu)造函數(shù)),如果父類中的屬性都能用到可以用parent::直接調(diào)用父類中的構(gòu)造函數(shù) public function __construct($name,$country) { //parent::直接調(diào)用父類中的構(gòu)造函數(shù) //parent::__construct($name,$height,$team,$playNum,$weight); $this->name = $name; $this->country = $country; } public function from() { return "{$this->name} come from {$this->country}"; } }
$zs = new CnPlayer('ZhangSiRui','China'); echo $zs->from();
__construct()雖然方便,但是每次類實例化一次 就自動調(diào)用一次就多占用一份內(nèi)存,一些需要頻繁調(diào)用,可以用靜態(tài)類static來實現(xiàn),不管調(diào)用多少次,都只占用一份內(nèi)存,但是
靜態(tài)成員不自動進(jìn)行銷毀,而實例化的則可以銷毀。
<?php class User { public static $name = '胡歌'; protected $_config = [ 'auth_on'=>'true', 'auth_type'=>1//認(rèn)證方式 1 實時認(rèn)證 2位登錄認(rèn)證 ]; public static $nation = 'China'; private $salary; static $count = 0;//記錄一登錄用戶的總數(shù) public function __construct($name,$salary) { //靜態(tài)成員與類的實例無關(guān),不能用$this訪問,用self::類的引用 訪問靜態(tài)成員 self::$name = $name; $this->salary = $salary; self::$count ++; } //靜態(tài)方法中調(diào)用靜態(tài)屬性(靜態(tài)方法中不能用$this->調(diào)用普通屬性) public static function getCount(){ return sprintf('目前該網(wǎng)站的在線人數(shù)為%d<br>',self::$count); } public function getConfig() { return sprintf('認(rèn)證開關(guān):%s<br>,認(rèn)證類型%d',$this->_config['auth_on'],$this->_config['auth_type']); } } ?>
靜態(tài)成員可以在未實例化的情況下就直接調(diào)用,因為靜態(tài)成員都是類級別的存在:
<?php require 'User.php'; echo User::$name;
這個情況下是可以直接輸出的,但是上面定義的__construct()中的$count++在未實例化類之前是不生效的,且靜態(tài)成員不自動進(jìn)行銷毀,而實例化的則可以銷毀:
<?php require 'User.php'; $user = new User('古力娜扎',25000); $user2 = new User('迪麗熱巴',25000); $user3 = new User('騷瑞',25000);//這里實例化三次 $count++也就執(zhí)行了三次 輸出$count會為3 echo User::$name; //這里如果使用一個unset()銷毀,則普通成員全部銷毀,但靜態(tài)成員不自動進(jìn)行銷毀 unset($user); //echo $user->getConfig();//這里匯報一個致命錯誤,因為getConfig()方法已被銷毀,現(xiàn)在已不存在 echo User::$name;//靜態(tài)成員依舊可以正常輸出
autoload自動加載的時候需要考慮到命名空間的問題 Linux跟windows里面路徑中的目錄分隔符寫法不同,如果實現(xiàn)一個類自動加載能在Linux跟windows系統(tǒng)都能使用,就需要做一個特殊處理。
<?php spl_autoload_register(function($className){ //聲明一個變量 賦值為用__DIR__獲取當(dāng)前文件位置拼接上類所在的文件夾位置再拼接上類名稱加.php后綴的絕對路徑 $file =__DIR__.'\\controller\\'. str_replace('\\',DIRECTORY_SEPARATOR,$className).'.php'; //判斷是否是不是文件名且該文件是否不存在 if(!(is_file($file)&&file_exists($file))) { //如果為真 則拋出一個錯誤 throw new \Exception('文件名不合法或者不存在'); } //為假就返回文件絕對路徑 require $file; }); ?>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號