欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP面向对象编程视频资料分享

程序员文章站 2022-04-18 11:45:03
...
在面向对象的程序设计(英语:Object-oriented programming,缩写:OOP)中,对象是一个由信息及对信息进行处理的描述所组成的整体,是对现实世界的抽象。

在现实世界里我们所面对的事情都是对象,如计算机、电视机、自行车等。

对象的主要三个特性:

对象的行为:可以对 对象施加那些操作,开灯,关灯就是行为。

对象的形态:当施加那些方法是对象如何响应,颜色,尺寸,外型。

对象的表示:对象的表示就相当于身份证,具体区分在相同的行为与状态下有什么不同。

本课程通过讲述面向对象的基本概念以及相关的案例实践,让小伙伴们对面向对象有一个基本的认识,能够掌握把实际问题抽象成为类对象用以解决实际问题的方法,掌握面向对象的最重要的核心能力。

PHP面向对象编程视频资料分享

视频播放地址:http://www.php.cn/course/329.html

本视频难点:

1. __construct:

内置构造函数,在对象被创建时自动调用。见如下代码:

<? php
classConstructTest {
    private $arg1;
    private $arg2;
    public function __construct($arg1, $arg2) {
        $this->arg1 = $arg1;
        $this->arg2 = $arg2;
        print "__construct is called...\n";
    }
    public function printAttributes() {
        print '$arg1 = ' . $this->arg1 . ' $arg2 = ' . $this->arg2 . "\n";
    }
}
$testObject = new ConstructTest("arg1", "arg2");
$testObject->printAttributes();

运行结果如下:

Stephens-Air:Desktop$ php Test.php
__construct is called...
$arg1 = arg1 $arg2 = arg2

2. parent:

用于在子类中直接调用父类中的方法,功能等同于Java中的super。

<? php
classBaseClass {
    protected $arg1;
    protected $arg2;
    function __construct($arg1, $arg2) {
        $this->arg1 = $arg1;
        $this->arg2 = $arg2;
        print "__construct is called...\n";
    }
    function getAttributes() {
        return '$arg1 = ' . $this->arg1 . ' $arg2 = ' . $this->arg2;
    }
}
class SubClass extends BaseClass {
    protected $arg3;
    function __construct($baseArg1, $baseArg2, $subArg3) {
        parent::__construct($baseArg1, $baseArg2);
        $this->arg3 = $subArg3;
    }
    function getAttributes() {
        return parent::getAttributes() . ' $arg3 = ' . $this->arg3;
    }
}
$testObject = new SubClass("arg1", "arg2", "arg3");
print $testObject->getAttributes() . "\n";

运行结果如下:

Stephens-Air:Desktop$ php Test.php
__construct is called...
$arg1 = arg1 $arg2 = arg2 $arg3 = arg3

以上就是PHP面向对象编程视频资料分享的详细内容,更多请关注其它相关文章!