您的位置:首页 > 编程语言 > PHP开发

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>


查看运行结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: