• PHP面向对象重要知识点----------第一部分


    1. __construct: 

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

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

    2. parent: 

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

    <?php
    class BaseClass {
    protected $arg1;
    protected $arg2;

    function __construct($arg1, $arg2) {
    $this->arg1 = $arg1;
    $this->arg2 = $arg2;
    print "__construct is called... ";
    }
    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()." ";

    3. self:

         在类内调用该类静态成员和静态方法的前缀修饰,对于非静态成员变量和函数则使用this。 

    复制代码
  • 相关阅读:
    Direct UI 思想阐述(好多相关文章)
    GetSystemTimeAsFileTime讲解(从1601年1月1日到目前经过的纳秒)
    WPF的消息机制
    CEdit 样式与消息 解析
    vcredist_x86.exe 静默安装方法
    iOS 开发问题集锦(一)
    EM算法详解
    BCP导入导出MsSql
    为什么不能在子类或外部发布C#事件
    HTML5 拖放及排序的简单实现
  • 原文地址:https://www.cnblogs.com/cgdblog/p/7325559.html
Copyright © 2020-2023  润新知