1. __construct:
内置构造函数,在对象被创建时自动调用。见如下代码:
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。