定義類
class class_name [extends partclass_name]
{
public private protected var property_name = value;
public private protected function method_name (){}
}ide
建立對象
$Obj = new Employee();函數
//使用->訪問對象成員
$Obj->Name = 'Flower';
$Obj->ShowName();this
Static 關鍵字 純粹通常用途
class MyMath
{
public static function Cubic($x)
{
return $x*$x;
}
}spa
訪問
echo MyMath::Cubic('5');對象
類常數const繼承
class Circle
{
const PI=3.14
public $Radius;io
public function ShowArea()
{
echo $this->Radius*self::PI;
}
$Obj = new Circle();
$Obj->Radius = 10;
$Obj->ShowArea();
}function
構造函數和析構函數
function _construct($str){$this->Name = $str;}class
function _destruct(){$this->Name = NULL}命名空間
//PHP7 匿名類
$Obj = new class('小紅豆')
{
public $Name;
function _construct($){$this->Name = $str;}
}
繼承 extends關鍵字
覆蓋 override
調用父類 parent::
方法前加final 表示子類不能覆蓋子類成員
namespace \namespace my\nameuse my\name as MN;//命名空間別名