本文目錄一覽:
PHP類的構造方法
構造方法是類中的一個特殊方法。當使用 new 操作符創建一個類的實例時,構造方法將會自動調用,其名稱必須是 __construct() 。所以通常用它執行一些有用的初始化任務。該方法無返回值。
如果子類中定義了構造函數則不會暗中調用其父類的構造函數。要執行父類的構造函數,需要在子類的構造函數中調用 parent::__construct()。
與構造方法對應的就是析構方法,析構方法會在某個對象的所有引用都被刪除或者當對象被顯式銷毀之前執行的一些操作或者功能。析構函數不能帶有任何參數,其名稱必須是 __destruct() 。
同樣,如果子類中定義了析構函數則不會暗中調用其父類的析構函數。要執行父類的析構函數,需要在子類的析構函數中調用 parent::__destruct()。
注意:在析構函數中拋出一個異常會導致致命錯誤。
?php
class Construct{
protected $a;
protected $b;
function __construct($a,$b){ //定義構造函數
$this-a=$a;
$this-b=$b;
}
function __destruct(){ //重新定義為初始值
$result=0;
echo ‘恢復乘積的初始值:’.$result;
}
}
class son extends construct{
private $c;
function __construct($c){
parent::__construct(6,10); //調用父類的構造方法,使用方法:parent::__construct(); 有參數加參數
$this-c=$c;
}
function show(){
$result=$this-a*$this-b*$this-c;
print ‘abc的乘積是:’.$result.’ ‘;
}
function __destruct(){
parent::__destruct(); //調用父類的析構函數,使用方式:parent::__destruct();
}
}
$test=new son(12); //實例化對象
$test-show(); //abc的乘積是:720 恢復乘積的初始值:0
?
如何在PHP中定義一個類
當你聲明一個類,你需要列出對象應有的所有變數和所有函數?被稱為屬性和方法圖1中顯示了一個類的構成. 注意在大括弧({})內你只能聲明變數或者函數. 圖2中顯示了如何在一個類中定義三個屬性和兩個方法.
以下為引用的內容:
class Name extends Another Class
{
Access Variable Declaration
Access Function Declaration
}
name = $name;
$this-password = $password;
$this-lastLogin = time();
$this-accesses++;
}
// 獲取最後訪問的時間
function getLastLogin()
{
return(date(“M d Y”, $this-lastLogin));
}
}
//創建一個對象的實例
$user = new User(“Leon”, “sdf123”);
//獲取最後訪問的時間
print($user-getLastLogin() .”\n”);
//列印用戶名
print(“$user-name\n”);
?
當你聲明屬性,你不需要指明數據類型. 變數可能是整型,字元串或者是另一個對象,這取決於實際情況.在聲明屬性時增加註釋是一個好主意,標記上屬性的含義和數據類型.
當你聲明一個方法,你所做的和在類外部定義一個函數是一樣的. 方法和屬性都有各自的命名空間. 這意味著你可以安全地建立一個與類外部函數同名的方法,兩者不會衝突. 例如,一個類中可以定義一個名為date()的方法. 但是你不能將一個方法命名為PHP的關鍵字,如for或者while.
類方法可能包含PHP中所謂的type hint. Type hint 是另一個傳遞參數給方法的類的名字. 如果你的腳本調用方法並傳遞一個不是類的實例的變數,PHP將產生一個」致命(fatal)錯誤」 . 你可能沒有給其它類型給出type hint,就像整型,字元串,或者布爾值. 在書寫的時候, type hint是否應當包含數組類型仍存在爭議.
Type hint是測試函數參數或者運算符的實例的數據類型的捷徑. 你可能總是返回這個方法. 確認你強制讓一個參數必須是哪種數據類型,如整型. 圖3 確保編譯類只產生Widget的實例
以下為引用的內容:
<?php
//組件
class Widget
{
public $name=’none’;
public $created=FALSE;
}
//裝配器
class Assembler
{
public function make(Widget $w)
{
print(“Making $w-name\n”);
$w-created=TRUE;
}
}
//建立一個組件對象
$thing = new Widget;
$thing-name = ‘Gadget’;
//裝配組件
Assembler::make($thing);
?>
除了傳遞參數的變數外,方法含有一個特殊的變數. 它代表類的個別實例. 你應當用這個來指向對象的屬性和其它方法.一些面向對象的語言假設一個不合格的變數提交給本地屬性,但在PHP中方法的任何變數只是在方法的一定範圍內. 注意在User類的構造函數中這個變數的使用圖2.
php創建一個類,在類中聲明一個數組存放另一個對象,為什麼無法調用數組中對象的方法?
//新建一個類User用來存放這,三個數據
//結果放到一個User數組中,你看這可以嗎?
//還是說要放到一個list中?
public class ObjectTest {
public static void main(String[] args) {
String[] id = new String[8];
String[] type = new String[8];
String[] username = new String[8];
User[] users = getUsers(type, id, username);
}
private static User[] getUsers(String[] type, String[] id, String[] username) {
User[] users = new User[type.length];
for (int i = 0; i type.length; i++) {
users[i] = new User(id[i], type[i], username[i]);
}
return users;
}
}
class User {
public User(String id, String tpye, String username) {
this.tpye = tpye;
this.id = id;
this.username = username;
}
private String tpye;
private String id;
private String username;
public String getTpye() {
return this.tpye;
}
public String getId() {
return this.id;
}
public String getUsername() {
return this.username;
}
public void setTpye(String tpye) {
this.tpye = tpye;
}
public void setId(String id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/309654.html