分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

PHP学习 Object Oriented 面向对象 OO

发布时间:2023-09-06 01:49责任编辑:郭大石关键词:PHP面向对象

定义类
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;//命名空间别名

PHP学习 Object Oriented 面向对象 OO

原文地址:https://www.cnblogs.com/kaixin110/p/8819533.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved