Blogger Information
Blog 37
fans 0
comment 0
visits 20801
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP编程第十七课:面向对象编程-PHP培训九期线上班
渡劫小能手
Original
507 people have browsed it

一、手抄课堂笔记: 1203.md (必做)

2019-12-07_235553.jpg

2019-12-07_235603.jpg

二、编程代码,抄写课堂案例,也可以自定义(选做)

1、demo1


实例

<?php
class test1
{
    public  $name = '孙悟空';
    public  function  getName()
    {
        $obj = new test1;
        return $obj->name . '归来';
    }
}
$obj = new test1();
echo $obj->name;
echo '<br>';
echo $obj->getName();

运行实例 »

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

2019-12-08_001547.jpg

2、demo2

 

实例

<?php
class test2
{
    public  $name = '孙悟空';
    public  $role = '打手';
    public  function  getName()
    {
//        $obj = new self();
        return $this->name . '取经';
    }
    public  function  getRole()
    {
//        $obj = new self();
        return $this->role ;
    }
}
$obj = new test2();
echo $obj->name;
echo '<br>';
echo $obj->getName();
echo '<br>';
echo $obj->getRole();

运行实例 »

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

2019-12-08_003035.jpg

3、demo3

实例

<?php
// 1. 创建类
class test3
{
    // 2. 添加类成员
    public  $name ;
    public  $role ;
    public  function  getInfo()
    {
        return '我是:' . $this->name . $this->role;
    }
    public function __construct($name,$role)
    {
        $this->name = $name;
        $this->role = $role;
        echo $this->getInfo();
    }
}
// 3. 访问类成员
$obj = new test3('PHP中文网','学生');
echo '<br>';

运行实例 »

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

2019-12-08_104653.jpg

4、demo4

实例

<?php
// 1. 创建类
class test4
{
    // 2. 添加类成员
    public  $site ;
    private  $role ;
    public  function  getInfo()
    {
        return '我是:' . $this->site . $this->role;
    }
    public function __construct($name,$role)
    {
        $this->site = $name;
        $this->role = $role;
        echo $this->getInfo();
    }
    public function __get($name)
    {
        $username = $_GET['username'] ?? '';
        if (isset($username) && $username === 'admin') {
            return isset($this->$name) ? $this->$name : '属性未定义';
        } else {
            return '无权访问';
        }
    }
}
// 3. 访问类成员
$obj = new test4('PHP中文网','学生');
echo '<br>';
echo $obj->role;
echo '<br>';
echo $obj->tiantian;

运行实例 »

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

2019-12-08_113757.jpg

5、demo5

实例

<?php
// 1. 创建类
class test5
{
    // 2. 添加类成员
    public  $site ;
    private  $role ;
    public  function  getInfo()
    {
        return '我是:' . $this->site .'职业:'. $this->role;
    }
    public function __construct($name,$role)
    {
        $this->site = $name;
        $this->role = $role;
    }
}

class test5_1 extends test5
{
    private $course;
    public function __construct($name, $role, $course)
    {
        parent::__construct($name, $role);
        $this->course = $course;
    }
    public  function  getInfo()
    {
        return parent::getInfo() . ',负责的是:' . $this->course;
    }
}

// 3. 访问类成员
$obj = new test5_1('PHP中文网','学生','听课');
echo $obj->getInfo();
echo '<br>';

运行实例 »

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

2019-12-08_115950.jpg

6、demo6

实例

<?php
// 1. 创建类
trait Test
{
    public  function  getInfo()
    {
        return '我是:' . $this->site .'职业:'. $this->role;
    }
}
class Test6
{
    // 2. 添加类成员
    public  $site ;
    private  $role ;
    use Test;
    public function __construct($name,$role)
    {
        $this->site = $name;
        $this->role = $role;
    }
}

class Test6_1 extends Test6
{
    private $course;
    public function __construct($name, $role, $course)
    {
        parent::__construct($name, $role);
        $this->course = $course;
    }
    public  function  getInfo()
    {
        return parent::getInfo() . ',负责的是:' . $this->course;
    }
}

// 3. 访问类成员
$obj = new Test6('PHP中文网','好学生');
echo $obj->getInfo();
echo '<br>';
$obj = new Test6_1('PHP中文网','学生','听课');
echo $obj->getInfo();
echo '<br>';

运行实例 »

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

2019-12-08_121112.jpg

7、demo7

实例

<?php
// 1. 创建类
interface iTest
{
    public function getInfo();
    public function hello();
}
class Test7 implements iTest
{
    // 2. 添加类成员
    public  $site ;
    private  $role ;

    public function __construct($site,$role)
    {
        $this->site = $site;
        $this->role = $role;
    }
    public  function  getInfo()
    {
        return '我是:' . $this->site .'职业:'. $this->role;
    }
    public function hello()
    {
        return '大家晚上好';
    }
}


// 3. 访问类成员
$obj = new Test7('PHP中文网','学生');
echo $obj->getInfo();
echo '<br>';
echo $obj->hello();

运行实例 »

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

2019-12-08_122451.jpg

8、demo8

实例

<?php
// 1. 创建类
abstract class Chouxiang
{
    abstract public function getInfo();
    public function hello()
    {
        return '大家早上好';
    }
}
class Test8 extends Chouxiang
{
    // 2. 添加类成员
    public  $site ;
    private  $role ;

    public function __construct($site,$role)
    {
        $this->site = $site;
        $this->role = $role;
    }
    public  function  getInfo()
    {
        return '我是:' . $this->site .'职业:'. $this->role;
    }

}


// 3. 访问类成员
$obj = new Test8('PHP中文网','小学生');
echo $obj->getInfo();
echo '<br>';
echo $obj->hello();

运行实例 »

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

2019-12-08_123328.jpg

三、对着天空,将OOP编程的三大步骤,大声的喊10遍

四、总结

1、OOP三步:创建类,添加类成员,访问类成员

2、self 是类名的引用, 始终与当前的类名绑定;$this 是当前类的实例的引用,它始终与当前类的实例绑定

3、构造方法: 类实例的初始化;自动完成在类实例创建过程中的操作

4、__get($name)和__set($name, $value)当访问未定义或不可见的属性/方法时, 重载方法会自动调用。这里面的$name是对象访问的成员传进来的这个成员名字。

5、当前类中的同名方法 > trait类中的同名方法  > 父类中的同名方法

6、对象的模板是类, 类的模板是接口,接口中定义的方法必须在工作类中全部实现

7、抽象类:有抽象方法, 也是已实现的方法,统统不能实例化, 原因就是内部有抽象方法

Correcting teacher:天蓬老师天蓬老师

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!