php 封裝 知識點

來源:互聯網
上載者:User

標籤:var_dump   代號   set   setname   round   物件導向   之間   ret   struct   


類
由眾多個物件抽象出來的
對象
由類執行個體化出來的

成員變數
成員方法
成員屬性

存取修飾詞
public 公有的
protected 受保護的
private 私人的

建構函式
1.寫法特殊
2.執行時間特殊

物件導向的三大特性 繼承 封裝 多態

1.封裝
目的:為了使一個類更加安全
做法:
1.將成員變數變為私人的
2.在類裡面做方法來間接訪問成員變數
3.在方法裡面加控制


class Ren
{
public $code; //人員代號
private $name; //人員名稱
private $sex; //人員性別
private $age; //人員年齡,必須在18-50周歲之間

//構造方法,用來初始化性別
function __construct($s)
{
$this->sex = $s;
}

/*//造一個修改年齡的方法
function SetAge($a)
{
//加控制
if($a>18 && $a<50)
{
$this->age = $a;
}
}
//造一個擷取age值的方法
function GetAge()
{
return $this->age;
}*/

//給變數賦值的魔術方法
function __set($n,$v)
{
if($n=="age")
{
if($v>18 && $v<50)
{
$this->$n = $v;
}
}
else
{
$this->$n = $v;
}
}

//__get魔術方法,用來取變數值的
function __get($n)
{
return $this->$n;
}
}

//造對象
$r = new Ren("男");
//$r->SetAge(30);
//$r->__set("age",30);

$r->age = 30; //自動調用__set()方法,將變數名當做第一個參數,將等號右邊的值當做第二個參數
$r->name = "張三";

//echo $r->sex; //自動調用__get()方法,將變數名當做參數
//var_dump($r);

class Dog
{
private $name;

function Jiao()
{
echo $this->name."在叫";
}

function SetName($n)
{
$this->name = $n;
return $this;
}

}

$d = new Dog();
$d->SetName("旺財")->Jiao();

 

//2.繼承

//3.多態

 

 

 

 

 


?>

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.