<?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();
?