PHP类和对象
本节介绍
- PHP类的定义
- PHP对象的创建
- 构造函数和析构函数
- 作用域
- PHP类的静态成员
- PHP类的继承
- PHP对象转JSON
1. PHP类的定义
- PHP中定义类语法格式:
-
class classname { public $property [=value] ; function functionName (args) { } }
-
- 类的成员(属性和方法)被调用时,可使用
$this->成员来调用
2. PHP对象的创建
- 使用new关键字创建对象
$对象名 = new classname ;
- 使用“$对象名->成员”访问对象成员
- 示例
-
创建对象
-
<?php //定义一个类Person class Person { public $name ; public $age ; public $height ; public function say() { //$this是伪变量,可以在类内部访问类的成员 //属性前面的$省略 echo "my name is ".$this->name ; } } //创建Person类的对象(用new关键字) $person1 = new Person ; $person1->name = "carry" ; $person1->age = 15 ; $person1->height = 160 ; //查看结构 var_dump($person1) ; ?> -
访问属性及调用方法
-
<?php //定义一个类Person class Person { public $name ; public $age ; public $height ; public function say() { echo "my name is ".$this->name ; } } //创建Person类的对象 $person1 = new Person ; $person1->name = "carry" ; $person1->age = 15 ; $person1->height = 160 ; //访问属性 echo "<br/>" ; echo "$person1->name" ; //调用方法 echo "<br/>" ; $person1->say() ; ?>
-
3. PHP构造函数
- 构造函数是在类中起到初始化的作用
- 构造函数的生成方法与其他函数一样只是其名称必须是
__construct(); - 一个类中只能声明一个构造函数
- 语法格式:
-
function __construct(参数) { ...... }
-
- 示例
-
<?php //定义一个类Person class Person { public $name ; public $age ; public $height ; //构造函数 function __construct($name, $age, $height) { $this->name = $name ; $this->age = $age ; $this->height = $height ; } public function say() { //$this是伪变量,可以在类内部访问类的成员 //属性前面的$省略 echo "my name is ".$this->name."<br/>" ; } } $person1 = new Person("carry", 15, 160) ; //自动调用对应的构造函数 $person2 = new Person("tom", 20, 170) ; $person1->say() ; $person2->say() ; ?>
-
4. 析构函数
- 析造函数是在类对象生命结束的时候,由系统自动调用释放在构造函数中分配的资源,如关闭文件、释放结果集等等
- 析构函数不能带有任何参数,其名称必须是
__destruct(); - 析构函数在对象调用结束时候自动调用
- 语法格式:
-
function __destruct() { //释放资源代码 ...... }
-
- 示例
-
<?php //定义一个类Person class Person { public $name ; public $age ; public $height ; //构造函数 function __construct($name, $age, $height) { $this->name = $name ; $this->age = $age ; $this->height = $height ; } public function say() { //$this是伪变量,可以在类内部访问类的成员 //属性前面的$省略 echo "my name is ".$this->name."<br/>" ; } //析构函数 function __destruct() { echo $this->name."调用析构函数"."<br/>" ; } } $person1 = new Person("carry", 15, 160) ; //自动调用对应的构造函数 $person2 = new Person("tom", 20, 170) ; $person1->say() ; $person2->say() ; ?>
-
5. 魔术方法
- PHP将所有以
__(两个下划线)开头的类方法保留为魔术方法 - 所以在自定义类方法时,建议不要以
__为前缀
6. 访问类型
- pbclic:公共的(公共修饰符)类内部与类外部都可以访问
- private:私有的(私有修饰符)只能在类内部访问
- protected:受保护的(保护成员修饰符)子类可以访问,类外部不可以访问
7. 类的Static成员
- Static表示静态,声明类属性或方法为
static,就可以不实例化类而直接访问 - 在类外部,静态成员使用
类名::调用,不能使用-> - 在类内部,静态成员使用
self::调用,不能使用$this - 示例
-
<?php //定义一个类Person class Person { public $name ; public $age ; public $height ; //人数(静态成员只初始化一次) public static $count = 0 ; function __construct($name, $age, $height) { $this->name = $name ; $this->age = $age ; $this->height = $height ; //人数加1 self::$count++ ; } //静态方法 public static function getCount() { echo "目前人数为:".self::$count."<br/>" ; } } $person1 = new Person("carry", 15, 160) ; $person2 = new Person("tom", 20, 170) ; //当前人数 echo(Person::$count) ; echo "<br/>" ; Person::getCount() ; ?>
-
8. PHP类的继承
- 使用关键字
extends声明一个类继承自父类 - PHP不支持多继承,即一个类只能有一个基类
- 子类继承父类的非私有成员,包括构造函数
- 子类可以重写父类的方法和属性,可使用
parent::来访问被覆盖的方法和属性 - 范围解析操作符
::,可以用于访问静态成员,类常量,还可以用于覆盖类中的属性和方法 - 示例
-
子类继承父类
-
<?php //定义一个类Person class Person { public $name ; public $age ; public $height ; //人数(静态成员只初始化一次) public static $count = 0 ; //构造函数 function __construct($name, $age, $height) { $this->name = $name ; $this->age = $age ; $this->height = $height ; //人数加1 self::$count++ ; } public function say() { echo "my name is ".$this->name."<br/>" ; } //静态方法 public static function getCount() { echo "目前人数为:".self::$count."<br/>" ; } } //Person类的子类 class Student extends Person { } $person1 = new Person("carry", 15, 160) ; $person2 = new Person("tom", 20, 170) ; $std = new Student("amy", 18, 165) ; $std->say() ; ?> -
子类重写父类方法
-
<?php //定义一个类Person class Person { public $name ; public $age ; public $height ; //人数(静态成员只初始化一次) public static $count = 0 ; //构造函数 function __construct($name, $age, $height) { $this->name = $name ; $this->age = $age ; $this->height = $height ; //人数加1 self::$count++ ; } public function say() { echo "my name is ".$this->name."<br/>" ; } //静态方法 public static function getCount() { echo "目前人数为:".self::$count."<br/>" ; } } //Person类的子类 class Student extends Person { //重写say方法 public function say() { //在重写的方法内部访问父类方法 parent::say() ; //重写 echo "i am student, my name is ".$this->name."<br/>" ; } } $person1 = new Person("carry", 15, 160) ; $person2 = new Person("tom", 20, 170) ; $std = new Student("amy", 18, 165) ; $std->say() ; ?> -
子类重写构造函数
-
<?php //定义一个类Person class Person { public $name ; public $age ; public $height ; //构造函数 function __construct($name, $age, $height) { $this->name = $name ; $this->age = $age ; $this->height = $height ; } } //Person类的子类 class Student extends Person { //定义学号 public $number ; //构造函数重写 function __construct($name, $age, $height, $number) { // $this->name = $name ; // $this->age = $age ; // $this->height = $height ; //调用父类的构造函数 parent::__construct($name, $age, $height) ; $this->number = $number ; } } $std = new Student("amy", 18, 165, "12345") ; var_dump($std) ; ?>
-
9. PHP对象转JSON字符串
- 使用json_encode函数可以将对象或者对象数组转化为JSON字符串
- **注意:**非Public的属性无法输出
- 示例
-
<?php //定义一个类Person class Person { public $name ; public $age ; public $height ; //构造函数 function __construct($name, $age, $height) { $this->name = $name ; $this->age = $age ; $this->height = $height ; } } //Person类的子类 class Student extends Person { //定义学号 public $number ; //构造函数重写 function __construct($name, $age, $height, $number) { // $this->name = $name ; // $this->age = $age ; // $this->height = $height ; //调用父类的构造函数 parent::__construct($name, $age, $height) ; $this->number = $number ; } } $std = new Student("amy", 18, 165, "12345") ; //一个对象进行json输出 $json = json_encode($std) ; echo($json) ; ?> -
<?php //定义一个类Person class Person { public $name ; public $age ; public $height ; //构造函数 function __construct($name, $age, $height) { $this->name = $name ; $this->age = $age ; $this->height = $height ; } } //Person类的子类 class Student extends Person { //定义学号 public $number ; //构造函数重写 function __construct($name, $age, $height, $number) { // $this->name = $name ; // $this->age = $age ; // $this->height = $height ; //调用父类的构造函数 parent::__construct($name, $age, $height) ; $this->number = $number ; } } $std = new Student("amy", 18, 165, "12345") ; //一个对象数组进行json输出 $arr = array($person1, $person2, $std) ; $json = json_encode($arr) ; echo($json) ; ?>
-