__set()魔法方法

将数据写入无法访问的属性时,运行__set()。

PHP get和set 魔术方法示例

<?php
class MyClass
{
private $fname;
private $lname;
private $email;

//设置名字
public function setFName($fname)
{
$this->fname = $fname;
}

//获取名字
public function getFName()
{
return $this->fname;
}

//设置姓
public function setLName($lname)
{
$this->lname = $lname;
}

//获取姓
public function getLName()
{
return $this->lname;
}

//设置邮箱地址

public function setEmail($email)

{

$this->email = $email;

}

 

//获取邮箱地址

public function getEmail()

{

return $this->email;

}

}

 

$user = new MyClass();

$user->setFName('Jack');

$user->setLName('Ma');

$user->setEmail('jma@gamil.com');

echo '名字: ' . $user->getFName().' </br>姓: ' . $user->getLName() .'</br> 邮箱: ' . $user->getEmail();

?>
PHP get和set方法是什么

__get()魔法方法

__get()魔术方法从无法访问的属性读取数据。

日期:2020-04-18 01:09:15 来源:oir作者:oir