標籤:關聯陣列 nbsp 索引 對象 var_dump prot ret 面向 function
首先需要定義數組,$attr = array(直接給元素1,2,3)索引數組
關聯陣列 $attr = array("one"=>1,2,3)
for($i=0;$i<count($attr);$i++) for迴圈遍曆索引數組 不能遍曆關聯陣列
foreach遍曆 關聯索引都可以遍曆
foreach($attr as $k=>$v)
{
$v;
}
物件導向 類 和 對象
例子:
$yuan = new Yuan();
$yuan->banjing = 10;
echo $yuan->MianJi();
var_dump($yuan);
//$this關鍵字在類裡面代表該對象
//造一個大圓
$maxyuan = new Yuan();
$maxyuan->banjing = 10;
//造一個小圓
$minyuan = new Yuan();
$minyuan->banjing = 5;
echo $maxyuan->MianJi()-$minyuan->MianJi();
class YunSuan
{
public $a=10;
public $b=5;
//構造方法
function __construct($a1,$b1)
{
$this->a = $a1;
$this->b = $b1;
}
//析構方法,在對象記憶體釋放的時候執行
function __destruct()
{
echo "該對象釋放了";
}
private function Jia()
{
return $this->a+$this->b;
}
function Jian()
{
return $this->a-$this->b;
}
function Cheng()
{
return $this->a*$this->b;
}
function Chu()
{
return $this->a/$this->b;
}
}
//造對象
$y = new YunSuan(10,5);
var_dump($y);
echo $y->Chu();
//存取修飾詞
//public 公有的,任何地方都可以訪問
//protected 受保護的,只能在該類或該類的子類中訪問
//private 私人的,只能在該類中訪問
//__開頭的方法在物件導向裡面成為魔術方法
//建構函式
//1.寫法特殊:方法名特殊
//2.執行時間特殊:造對象的時候就執行
//對對象裡面的成員進行初始化
1211php物件導向