2016年12月11日 php物件導向

來源:互聯網
上載者:User

標籤:修飾符   3.1   物件導向   方式   初始化   面向   面積   存取修飾詞   開頭   

物件導向
1.類(由眾多個物件中抽象出來的)
2.對象(一切皆對象,由類執行個體化出來的)。

類:
求圓的面積
面向過程的方式
1.將圓抽象為一個類

2.執行個體化對象
class Qiu
{
var $banjing;
function Biaomianji()
{
return 4*3.14*$this->banjing*$this->banjing;//this關鍵字代表該對象
}
}
$qiu=new Qiu;
$qiu->banjing=5;
echo $qiu->Biaomianji();

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.執行時間特殊:造對象的時候就執行

對對象裡面的成員進行初始化

2016年12月11日 php物件導向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.