1211php物件導向

來源:互聯網
上載者:User

標籤:關聯陣列   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物件導向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.