时间:2021-07-01 10:21:17 帮助过:7人阅读
classConstructTest {private$arg1;
private$arg2;
publicfunction__construct($arg1, $arg2) {$this->arg1 = $arg1;
$this->arg2 = $arg2;
print"__construct is called...\n";
}
publicfunctionprintAttributes() {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
用于在子类中直接调用父类中的方法
classBaseClass {protected$arg1;
protected$arg2;
function__construct($arg1, $arg2) {$this->arg1 = $arg1;
$this->arg2 = $arg2;
print"__construct is called...\n";
}
functiongetAttributes() {return'$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2;
}
}
classSubClassextendsBaseClass {protected$arg3;
function__construct($baseArg1, $baseArg2, $subArg3) {parent::__construct($baseArg1, $baseArg2);
$this->arg3 = $subArg3;
}
functiongetAttributes() {returnparent::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
classStaticExample {staticpublic$arg1 = "Hello, This is static field.\n";
staticpublicfunctionsayHello() {printself::$arg1;
}
}
print StaticExample::$arg1;
StaticExample::sayHello();
abstractclassBase {publicstaticfunctiongetInstance() {//这里的new static()实例化的是调用该静态方法的当前类。returnnewstatic();
}
abstractpublicfunctionprintSelf();}
classSubAextendsBase {publicfunctionprintSelf() {print"This is SubA::printSelf.\n";
}
}
classSubBextendsBase {publicfunctionprintSelf() {print"This is SubB::printSelf.\n";
}
}
SubA::getInstance()->printSelf();
SubB::getInstance()->printSelf();
运行结果如下:
Stephens-Air:Desktop$ php Test.php
This is SubA::printSelf.
This is SubB::printSelf.
static关键字不仅仅可以用于实例化。和self和parent一样,static还可以作为静态方法调用的标识符,甚至是从非静态上下文中调用。在该场景下,self仍然表示的是当前方法所在的类。见如下代码:
abstractclassBase {private$ownedGroup;
publicfunction__construct() {//这里的static和上面的例子一样,表示当前调用该方法的实际类。//需要另外说明的是,这里的getGroup方法即便不是静态方法,也会得到相同的结果。然而倘若//getGroup真的只是普通类方法,那么这里还是建议使用$this。$this->ownedGroup = static::getGroup();
}
publicfunctionprintGroup() {print"My Group is ".$this->ownedGroup."\n";
}
publicstaticfunctiongetInstance() {returnnewstatic();
}
publicstaticfunctiongetGroup() {return"default";
}
}
classSubAextendsBase {
}
classSubBextendsBase {publicstaticfunctiongetGroup() {return"SubB";
}
}
SubA::getInstance()->printGroup();
SubB::getInstance()->printGroup();
运行结果如下:
Stephens-Air:Desktop$ php Test.php
My Groupisdefault
My Groupis SubB
classTestClass {function__destruct() {print"TestClass destructor is called.\n";
}
}
$testObj = new TestClass();
unset($testObj);
print"Application will exit.\n";
运行结果如下:
Stephens-Air:Desktop$ php Test.php
TestClass destructoriscalled.
Applicationwillexit.
classInnerClass {public$id = 10;
publicfunctionprintSelf() {print'$id = '.$this->id."\n";
}
}
classOuterClass {public$innerClass;
publicfunction__construct() {$this->innerClass = new InnerClass();
}
publicfunction__clone() {$this->innerClass = clone$this->innerClass;
print"__clone is called.\n";
}
}
$outerA = new OuterClass();
print"Before calling to clone.\n";
$outerB = clone$outerA;
print"After calling to clone.\n";
$outerA->innerClass->id = 20;
print"In outerA: ";
$outerA->innerClass->printSelf();
print"In outerB: ";
$outerB->innerClass->printSelf();
运行结果如下:
Stephens-Air:Desktop$ php Test.php
Before calling to clone.
__clone is called.
After calling to clone.In outerA: $id=20In outerB: $id=10
以上就介绍了php面向对象要点[转],包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。