<?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;
?>
Вопрос:php: когда нужно использовать self :: method()?
Если изменение $this->newSubClass();
в self::newSubClass();
, он также работает, поэтому, когда мне нужно использовать $this->newSubClass();
, и при необходимости использовать self::newSubClass();
?
self :: is use on static methods/$ this используется для самого obj. –
и 'self' почти не имеют значения, как правило, вы хотите использовать' static'. См. Информацию об [конце статической привязки] (http://php.net/manual/en/language.oop5.late-static-bindings.php) – Orangepill