~~~~ 有胆量你就来跟着路老师卷起来! -- 纯干货,技术知识分享 ~~~~
路老师给大家分享PHP语言的知识了,旨在想让大家入门PHP,并深入了解PHP语言。

入门PHP就来我这(纯干货)07_开发语言

1 构造方法和析构方法

1.1 构造方法

当一个类实例化一个对象时,可能会随着对象初始化一些成员变量,构造方法就是在创建对象的时候初始化对象参数的方法。

<?php 
   class SportObject{ //定义运动类
    public $name;
    public $height;
    public $weight;
    public $age;
    public $sex;
   
    // 构造方法
    public function __construct($name,$height,$weight,$age,$sex){
        $this->name = $name;
        $this->height = $height;
        $this->weight = $weight;
        $this->age = $age;
        $this->sex = $sex;
    }

    public function playBasketball(){
        echo "姓名:".$this->name."<br>身高:".$this->height.
        "<br>体重:".$this->weight."<br>年龄:".$this->age.
        "<br>性别:".$this->sex;
    }
 }
 //创建实例的时候并初始化对象参数
 $sport = new SportObject('龙傲天','188','80','30岁','男');
 //方法调用采用 对象->方法
 echo $sport->playBasketball();
?>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

入门PHP就来我这(纯干货)07_PHP基础知识_02

1.2 析构方法

析构方法的作用和构造方法正好相反,是在对象被销毁时调用,作用是释放内存。

<?php 
   class SportObject{ //定义运动类
    public $name;
    public $height;
    public $weight;
    public $age;
    public $sex;
   
    /***
     * 构造方法
     */
    public function __construct($name,$height,$weight,$age,$sex){
        echo "<p style='color:green;'><b>对象创建时初始化参数,调用的是构造方法!</b></p>";
        $this->name = $name;
        $this->height = $height;
        $this->weight = $weight;
        $this->age = $age;
        $this->sex = $sex;
    }

    public function playBasketball(){
        echo "姓名:".$this->name."<br>身高:".$this->height.
        "<br>体重:".$this->weight."<br>年龄:".$this->age.
        "<br>性别:".$this->sex;
    }

    /***
     * 析构方法
     */

    public function __destruct(){
        echo "<p style='color:red;'><b>对象被销毁,调用的是析构方法!</b></p>";
    }
 }
 //创建实例的时候并初始化对象参数
 $sport = new SportObject('龙傲天','188','80','30岁','男');
 //方法调用采用 对象->方法
 echo $sport->playBasketball();
?>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

入门PHP就来我这(纯干货)07_PHP基础知识_03

注意:PHP使用的是一种“垃圾回收”机制,自动清除不在使用的对象,释放内存。也就是说即使不使用unset()函数,析构方法也会自动被调用,这里只是明确一下析构方法在何时被调用。一般情况下不需要手动创建析构方法。

2 继承和多态

2.1 继承

一句话解释:就是子类可以继承父类所有成员变量和方法,包括构造方法。

当子类被创建时,PHP会先在子类中查找构造方法,如果子类有自己的构造方法,PHP会先调用子类中的方法,当子类中没有构造方法,PHP会调用父类中的构造方法,这就是继承。

/**
 * 继承语法
 *
 */
class subClass extends superClass{
   //类体
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

 设计一个运动父类,两个子类打篮球和举重类:

<?php 
   class SportObject{ //定义运动类
    public $name;
    public $height;
    public $weight;
    public $age;
    public $sex;
   
    /***
     * 构造方法
     */
    public function __construct($name,$height,$weight,$age,$sex){
        echo "<p style='color:green;'><b>对象创建时初始化参数,调用的是构造方法!</b></p>";
        $this->name = $name;
        $this->height = $height;
        $this->weight = $weight;
        $this->age = $age;
        $this->sex = $sex;
    }

    public function showMe(){
        echo "姓名:".$this->name."<br>身高:".$this->height.
        "<br>体重:".$this->weight."<br>年龄:".$this->age.
        "<br>性别:".$this->sex;
    }
  }

  class PlayBasketBall extends SportObject{
    public $height;
    function __construct($name,$height){
        $this->height=$height;
        $this->name=$name;
    }
    function showMe(){
        return "子类PlayBasketBall的用户名:".$this->name.
        "子类PlayBasketBall的身高:".$this->height;
    }
  }
  class WeightLifting extends SportObject{
    public $height;
    function __construct($name,$height){
        $this->height=$height;
        $this->name=$name;
    }
    function showMe(){
        return "子类WeightLifting的用户名:".$this->name.
        "子类WeightLifting的身高:".$this->height;
    }
  }


  /**
   * 继承测试
   */
  $playBasketBall = new PlayBasketBall('路老师','190');
  $weightLifting = new WeightLifting('路TT','180');
  echo $playBasketBall->showMe()."<br>";
  echo $weightLifting->showMe()."<br>";

?>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.

入门PHP就来我这(纯干货)07_php_04

 2.2 多态

多态好比有一个成员方法让大家去游泳,这个时候有的人带游泳圈,有的人拿浮板,有的人什么也不带,纯粹喝水。虽然是同一个方法,但是产生了不同的形态,这就是多态。

比如一个汽车抽象类Car,它有一个获取速度的成员方法getSpeed()。现在有3个汽车品牌的子类,分别继承Car父类,并且都有一个获取速度的成员方法getSpeed()。三个不同子类,调用同一个方法产生三种不同的形态:

<?php 
   /***
    * 定义抽象类Car
    */
    abstract class Car{ 
    abstract function getSpeed();
  }

  class Toyota extends Car{
    function getSpeed(){
        return "Toyota speed";
    }
  }
  class Nissan extends Car{
    function getSpeed(){
        return "Nissan speed";
    }
  }
  class Tesla extends Car{
    function getSpeed(){
        return "Tesla speed";
    }
  }


  /**
   * 继承测试
   */
  $car = new Toyota();
  $speed = $car->getSpeed();
  echo $speed;

?>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.

入门PHP就来我这(纯干货)07_PHP基础知识_05

3 "$this->" 和 "::" 的使用

3.1 $this->

都知道,定义类的时候,你根本就不知道对象的名称是什么,这时如果想调用类中的方法,就要用伪变量"$this->"。"$this"意思就是本身,所以"$this->"只可以在类的内部使用。

<?php 
   /***
    * 定义抽象类Car
    */
    class Car{
        function test(){
            if(isset($this)){
                echo '$this的值为:'.get_class($this);
            } else {
                echo '$this未定义';
            }
        }
    }
    $class_name = new Car();
    $class_name->test();
   

?>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

入门PHP就来我这(纯干货)07_面向对象_06

 备注:get_class()函数返回对象所属类的名字,如果不是对象,返回false。

3.2 操作符 "::"

相对于伪变量$this只能在类的内部使用,操作符"::"更为强大,可以在没有声明任何实例的情况下访问类中的成员方法或成员变量。使用"::"操作符的通用格式为:

关键字::变量名/常量名/方法名

这里的关键字分为以下三种情况:

  • parent关键字:可以调用父类中的成员变量、成员方法和常量。
  • self关键字:可以调用当前类中的静态成员和常量。
  • 类名:可以调用本类中的变量、常量和方法。

下例子依次使用类名、parent关键字和self关键字来调用变量和方法。

 

<?php 
   /***
    * 定义抽象类Book
    */
    class Book{
        const NAME= 'computer';//定义常量NAME
        /**
         * 定义构造方法
         */
        function __construct(){
            echo "本年度图书类冠军为:".Book::NAME."<br>";
        }
    }
    class BookRank extends Book{
        const NAME = 'foreign language';
        function __construct(){
            parent::__construct();
            echo '本月图书类冠军为:'.self::NAME.'';
        }
    }
    $obj = new BookRank();

?>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

入门PHP就来我这(纯干货)07_php_07

4 数据隐藏 

4.1 三类访问权限关键字

面向对象编程的特点之一是封装性,即数据隐藏。此时就涉及了数据的访问权限,访问权限对应的关键字有三类:

名字

介绍

权限范围

public

公共成员

可以在程序中的任何位置被其他的类和对象调用,子类也可以继承和使用父类中所有的公共成员。

private

私有成员

只能在所属类的内部调用和修改,不可以在类外被访问,子类中也不可以。

protected

保护成员

仅自己和子类能够调用自己的变量和方法。

注意:对于成员方法,如果没有写关键字,那么默认就是public,建议以后写程序的时候都要带上关键字,这是一种良好的编程习惯。

4.2 静态变量(方法)

可以通过给变量(方法)添加static关键字来直接调用。调用静态成员的格式为:

关键字::静态成员

关键字可以是self,以及所在的类名。

<?php 

  class Book{
    static $bookNum = 0;//定义静态变量
  }
?>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

下篇文章 php对象的高级及应用。