<?php
class MyClass
{
public $prop1 = "I'm a class property!";
public function __construct()
{
echo 'The class "', __CLASS__, '" was initiated!<br />';
}
public function __destruct()
{
echo 'The class "', __CLASS__, '" was destroyed.<br />';
}
public function __toString()
{
echo "Using the toString method: ";
return $this->getProperty();
}
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()
{
return $this->prop1 . "<br />";
}
}
class MyOtherClass extends MyClass
{
public function __construct()
{
parent::__construct(); // Call the parent class's constructor
$this->newSubClass();
}
public function newSubClass()
{
echo "From a new subClass " . __CLASS__ . ".<br />";
}
}
// Create a new object
$newobj = new MyOtherClass;
?>
如果改變$this->newSubClass();
到self::newSubClass();
,它也可以,所以,當我需要使用$this->newSubClass();
,當需要使用self::newSubClass();
?
self ::用於靜態方法/ $這是在obj本身上使用的。 –
和'self'幾乎不是你的意思,通常你想用'static'。查看[晚期靜態綁定]的信息(http://php.net/manual/en/language.oop5.late-static-bindings.php) – Orangepill