定义类
class class_name [extends partclass_name]
{
public private protected var property_name = value;
public private protected function method_name (){}
}
创建对象
$Obj = new Employee();
//使用->访问对象成员
$Obj->Name = ‘Flower‘;
$Obj->ShowName();
Static 关键字 纯粹一般用途
class MyMath
{
public static function Cubic($x)
{
return $x*$x;
}
}
访问
echo MyMath::Cubic(‘5‘);
类常数const
class Circle
{
const PI=3.14
public $Radius;
public function ShowArea()
{
echo $this->Radius*self::PI;
}
$Obj = new Circle();
$Obj->Radius = 10;
$Obj->ShowArea();
}
构造函数和析构函数
function _construct($str){$this->Name = $str;}
function _destruct(){$this->Name = NULL}
//PHP7 匿名类
$Obj = new class(‘小红豆‘)
{
public $Name;
function _construct($){$this->Name = $str;}
}
继承 extends关键字
覆盖 override
调用父类 parent::
方法前加final 表示子类不能覆盖子类成员
namespace \
namespace my\name
use my\name as MN;//命名空间别名
原文地址:https://www.cnblogs.com/kaixin110/p/8819533.html