PHP 高级课程笔记 面向对象

前端之家收集整理的这篇文章主要介绍了PHP 高级课程笔记 面向对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

例一:
<div class="codetitle"><a style="CURSOR: pointer" data="92832" class="copybut" id="copybut92832" onclick="doCopy('code92832')"> 代码如下:

<div class="codebody" id="code92832">
<?PHP
// 类的定义
class User
{
// 属性,注意public、private、protected的作用范围
public $name = "hackbaby";
// 构造函数
function construct()
{
echo "construct
";
}
// 方法
function say()
{
echo "这是在类的本身调用:$this->name";
}
// 析构函数
function __destruct()
{
echo "destruct";
}
// 返回当前对象的描述信息 通过实例化的变量名调用例如本例中的$user
function
toString()
{
return "user class";
}
}
//实例化,如果构造函数有参数则用$user = new User('参数');
$user = new User();
echo $user->name . "
";
$user->say();
echo "
";
echo $user;
?>

例二: <div class="codetitle"><a style="CURSOR: pointer" data="84546" class="copybut" id="copybut84546" onclick="doCopy('code84546')"> 代码如下:
<div class="codebody" id="code84546">
<?PHP
class Fruit
{
protected $fruit_color;
protected $fruit_size; function setcolor($color)
{
$this->fruit_color = $color;
} function getcolor()
{
return $this->fruit_color;
} function setsize($size)
{
$this->fruit_size = $size;
} function getsize()
{
return $this->fruit_size;
} function save()
{
//代码
}
}
class apple extends Fruit
{
private $variety; function setvariety($type)
{
$this->variety = $type;
} function getvariety()
{
return $this->variety;
}
}
$apple = new apple();
echo $apple->setvariety('红富士');
echo $apple->getvariety();
echo "
";
echo $apple->setcolor('red');
echo $apple->getcolor();
echo "
";
echo $apple->setsize('特大');
echo $apple->getsize(); ?>

原文链接:https://www.f2er.com/php/29157.html

猜你在找的PHP相关文章