class ParentClass {
// описание полей и методов
}
class ChildClass extends ParentClass {
// описание дополнительных полей и методов
}
class Foo
{
public function printItem($string)
{
echo 'Foo: ' . $string . PHP_EOL;
}
public function printPHP()
{
echo 'PHP is great.' . PHP_EOL;
}
}
class Bar extends Foo
{
public function printItem($string)
{
echo 'Bar: ' . $string . PHP_EOL;
}
}
$foo = new Foo();
$bar = new Bar();
$foo->printItem('baz'); // Выведет: 'Foo: baz'
$foo->printPHP(); // Выведет: 'PHP is great'
$bar->printItem('baz'); // Выведет: 'Bar: baz'
$bar->printPHP(); // Выведет: 'PHP is great'
class ParentClass {
public function myMethod() {
// (действия)
}
}
class ChildClass extends ParentClass {
public function myMethod() {
// вызывется для объекта класса ChildClass
// вместо метода супер-класса MyMethod()
}
}
class Admin {
private $rights;
public function __construct($login, $password, $rights) {
$this->login = $login;
$this->password = $password;
$this->rights = $rights;
}
public function authorize() {
$_SESSION['login'] = $this->getLogin();
$_SESSION['rights'] = $this->getRights();
}
public function getLogin() {
return $this->login;
}
public function getRights() {
return $this->rights;
}
public function setRights($rights) {
$this->rights = $rights;
}
}
class User {
private $login;
private $password;
private $rights;
public function __construct($login, $password, $rights = 0) {
$this->login = $login;
$this->password = $password;
$this->rights = $rights;
}
public function getLogin() {
return $this->login;
}
public function getRights() {
return $this->rights;
}
public function setRights($rights) {
$this->rights = $rights;
}
public function authorize() {
$_SESSION['login'] = $this->getLogin();
}
}
public function __construct($login, $password, $rights) {
parent::__construct($login, $password);
$this->rights = $rights;
}
class A {
public $property = 'value';
}
class B extends A{
public function getProperty() {
return $this->property;
}
}
$b = new B();
echo "{$b->getProperty()}
"; // value