一、面向对象概念
1.概念
面向对象(Object Oriented,OO)其是以对象模型为基础进行的抽象过程,并在应用过程中形成了描述自己的抽象概念定义,包括对象、类、封装、继承以及多态等。
2.面向对象的三大特性
1、封装
隐藏对象的属性和实现细节,仅对外提供公共访问方式,将变化隔离,便于使用,提高复用性和安全性。
2、继承
提高代码复用性;
3、多态
父类或接口定义的引用变量可以指向子类或具体实现类的实例对象。提高了程序的拓展性。
二、类与对象
类通过class声明
对象是类的实例化
class people{
//属性
const constant = '常量';
private $sec="秘密";
public $height=123;
protected $age=10;
public $weight=64;
//构造函数
public function __construct(){
echo "构造函数初始化";
}
public function __desctruct(){
echo "虚构函数 收尾";
}
protected function age(){
return $this->age;
}
private function secret(){
return "这是我的秘密";
}
public function love(){
return "love food";
}
public function say(){
return "say you ";
}
}
class girl extends people{
function aa(a,b){
$str=parent::age();
return "继承了".$str;
}
function dd(){
}
}
$a=new A();
$girl=new girl();
类的元素
属性
常量 const constant = 'constant value'; A::constant;
static 静态属性和方法
构造函数和析构函数 __construct() __destruct exit();
方法
实例化 new A();
继承 extends
三、访问方法
$this->
self::test();
parent::test();
A::static_test();
四、访问控制与关键字
public 公有的 都可访问
private 私有的 不可访问 不可继承
protected 受保护的 不可访问 可继承
//关键字
static 可以直接使用 class::static 不能使用this self::
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有