Blogger Information
Blog 40
fans 0
comment 0
visits 37600
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP学习总结(8)类与继承——2019年09月30号20:00分
虎子爸爸
Original
654 people have browsed it

constr-1.png

子类代码——User.php

实例

<?php
namespace Phpstudy;
include 'Base.php';
class User extends Base{
    // 先定义三个类属性(变量)
    public $name;
    public $address;
    public $work;
    // 再做个构造方法进行类的初始化
    public function __construct($name,$address){
        //第一步:先执行父类的构造方法
		parent::__construct();
        // 先初始化类属性
        $this->name = $name;
        $this->address = $address;
        // 然后调用当前类里面的方法;
        echo $this->getInfo();

    }
    public function getInfo(){
        return "他的名字是--->".$this->name."<br>他的住址是--->".$this->address;
    }
}
?>
<!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>Document</title>
</head>
<body>
    <?php
    new User('虎子爸爸','郑州');
    ?>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

父类代码——Base.php

实例

<?php
namespace Phpstudy;
class Base{
    // 再做个构造方法进行类的初始化
    public function __construct(){
        echo "这是父类的初始化方法<br>";

    }

}

?>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:总结的不错, 在项目要灵活应用
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post