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...\n";
    }
    public function printAttributes() {
        print '$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2."\n";
    }
}
$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...\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";

3. self:

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

复制代码

转载于:https://www.cnblogs.com/cgdblog/p/7325559.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值