php物件導向程式設計示例學習筆記

2021-06-29 10:32:38 字數 1823 閱讀 3302

1、__get()和__set()方法

<?php

// put your code here

class animal

else

}public function __set($propertyname, $value)

}$pig = new animal();

$pig->name = "豬";

$pig->color = "白色";

$pig->age = "1歲";

echo "稱呼:".$pig->name."

"; echo "顏色:".$pig->color."

"; echo "年齡:".$pig->age."

"; ?>

2、__call()方法

<?php

// put your code here

class test

}$test = new test();

$test->demo("one", "two", "three");

echo "this is a test

"; ?>

3、clone物件

<?php

// put your code here

class animal

function getinfo()

}$pig = new animal("豬", "白色", "1歲");

//使用clone轉殖新物件pig2,和$pig物件具有相同的屬性和方法

$pig2 = clone $pig;

$pig2->getinfo();

?>

4、__clone()方法

<?php

// put your code here

class animal

function getinfo()

function __clone()

}$pig = new animal("豬", "白色", "1歲");

//使用clone轉殖新物件pig2,和$pig物件具有相同的屬性和方法

$pig2 = clone $pig;

$pig->getinfo();

$pig2->getinfo();

?>

5、__tostring()方法

<?php

// put your code here

//declare a ****** class

class testclass

public function __tostring()

}$class = new testclass('helloworld');

echo $class;

?>

6、const關鍵字

<?php

// put your code here

class myclass

}echo myclass::constant."\n";

$class = new myclass();

$class->showconstant();

?>

php學習筆記 PHP物件導向的程式設計

php物件導向的程式設計 php5 陣列和物件 都屬於php的復合型別 乙個變數可以儲存多個單元 物件比陣列更強大,不僅可以儲存多個資料,還可以將函式存在物件中 物件的三大特性 封裝 繼承 多型 物件導向程式設計 oop 符合軟體工程中的 重用性 靈活性 擴充套件性 物件導向和面向過程之間的區別 最...

PHP物件導向程式設計 筆記

在慕課網學習了建構函式 construct 在物件例項化後呼叫物件後第一時間觸發析構函式 destroy 在物件例項確認銷毀,且銷毀之前觸發 tostring 當物件被當做string使用時,這個方法會自動呼叫 即物件當做乙個變數使用時,會被觸發 invoke 當物件被當做方法使用時,這個方法會自動...

PHP學習筆記 物件導向

類的結構 class classname var attribute 新增屬性 function operation 宣告函式 建構函式 construct 析構函式 destruct 使用類的屬性 在乙個類中,可以訪問乙個特殊的指標 this。eg this attribute。在類的外部直接訪問...