PHP学习笔记 10 - 类
2017-11-24 08:49
274 查看
类
定义
类定义以关键字 class 开头,后面接类的名字,接着是一对大括号括起来的类体class MyClass { // ... }
类中可以定义方法(函数)和属性(变量)
class MyClass { public function method() { // ... } private $property = value; }
访问控制符:
private:仅在类中可以访问
protected:在基类与子类中可以访问
public:类内与类外都可以访问
$this 指代类对象自身
class MyClass { public function method() { echo $this->property; } private $property = value; }
使用 const 定义常量,常量默认为 public,通过ClassName::CONTANT访问常量,注意不加 $
class MyClass { const CONSTANT = value; }
使用 static 定义静态方法与属性,通过 ClassName::method() 与 ClassName::$property 访问静态方法与变量,
self 指代类自身
class MyClass { public static $x = 1; public static function method() { echo self::$x; } }
使用 spl_autoload_register() 自动加载类
spl_autoload_register(function ($class_name) { include $class_name . '.php'; });
构造函数:function __construct()
析构函数:function __destruct()
对象
创建类实例使用 new 操作符$myClass = new MyClass();
调用方法使用 -> 操作符
$myClass->method();
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Class Definition</title> </head> <body> <?php class Tank { // 构造函数 function __construct($shellCount) { $this->shellCount = $shellCount; echo 'construct<br>'; } // 析构函数 function __destruct() { echo 'destruct<br>'; } // 方法 public function fire(&$dest) { if ($this->shellCount <= 0) { echo 'No more shells!<br>'; return; } echo "Fire at (" . $dest['x'] . "," . $dest['y'] . ")<br>"; --$this->shellCount; } // 属性 private $shellCount = 0; // 常量 const MAX_LOAD_TON = 10; // 静态属性 private static $country = 'PRC'; // 静态方法 public static function country() { echo self::$country . "<br>"; } } // 使用 new 创建实例 $myTank = new Tank(10); $dest = array('x'=>100, 'y'=>100); for ($i = 0; $i < 11; ++$i) { $myTank->fire($dest); // 通过 object->method() 调用方法 } echo 'The maximum load for Tank is ' . Tank::MAX_LOAD_TON // 通过 ClassName::CONSTANT 访问类常量 . ' ton.<br>'; // 通过 ClassName::staticMethod() 访问静态方法 Tank::country(); ?> </body> </html>
查看运行结果
继承
使用 extends 实现继承通过 final 修饰的类不可以被继承
PHP中的方法默认为虚方法,在子类中可以重写
通过 final 修饰基类方法来禁止子类重写方法
PHP不支持多继承
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Class Extends</title> </head> <body> <?php class Tank { public function fire(&$dest) { if ($this->shellCount <= 0) { echo 'No more shells!<br>'; return; } echo "Fire at (" . $dest['x'] . "," . $dest['y'] . ")<br>"; --$this->shellCount; } private $shellCount = 10; } class SuperTank extends Tank { public function fire(&$dest) { echo "Emit laser wave at (" . $dest['x'] . "," . $dest['y'] . ")<br>"; } public function dig() { echo "Now dig into earth<br>"; } public function dive() { echo "Now dive into water<br>"; } } $superTank = new SuperTank(); $dest = array("x"=>100, "y"=>100); $superTank->fire($dest); $superTank->dig(); $superTank->dive(); ?> </body> </html>
查看运行结果
抽象类
使用 abstract 定义抽象类abstract 修饰的方法为抽象方法,抽象方法没有实现
具有抽象方法的类必须是抽象类
抽象类指定了子类必须实现其抽象方法
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Abstract Class</title> </head> <body> <?php abstract class Vehicle { abstract public function move(&$dest); } class Tank extends Vehicle { public function move(&$dest) { echo "Moving to (" . $dest['x'] . "," . $dest['y'] . ") using track<br>"; } } $tank = new Tank(); $dest = array('x'=>100, 'y'=>100); $tank->move($dest); ?> </body> </html>
查看运行结果
接口
使用 interface 定义接口使用 implements 实现接口
一个类可以实现多个接口,多个接口用逗号(
,)分隔
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Interface</title> </head> <body> <?php interface Movable { function move(&$dest); } class Tank implements Movable { public function move(&$dest) { echo "Moving to (" . $dest['x'] . "," . $dest['y'] . ")"; } } $tank = new Tank(); $dest = array('x'=>100, 'y'=>100); $tank->move($dest); ?> </body> </html>
查看运行结果
相关文章推荐
- PHP学习笔记(10)mysql常用命令
- php学习笔记(10):MYSQL数据库中的常用SQL语句
- PHP学习笔记10-学习制作PHP+MYSQL留言板(上)
- 韩顺平_PHP程序员玩转算法公开课(第一季)10_使用栈完成高级计算器(2)_学习笔记_源代码图解_PPT文档整理
- PHP学习笔记10——GD图片处理
- php 学习笔记--数组篇(10)
- PHP学习笔记【10】--PHP函数
- php学习笔记10-Exception Handling
- PHP学习笔记【10】_函数
- PHP学习笔记(10)—— 浮点数与运算优先级
- php学习笔记10--mongodb的数据增删改查
- 【小白笔记】PHP学习之路 (10) --字符串(2)
- PHP学习笔记 - 进阶篇(10)
- PHP学习笔记【10】--PHP函数
- PHP学习笔记(10) phpcms模块开发,让swfupload为我所用
- PHP学习笔记10-MYSQL在PHP5中的应用
- php学习笔记(10)—— 文件管理系统
- PHP学习笔记10:函数
- php yii 学习笔记
- lua学习笔记10:lua简单命令行