PHP OOP - Metode statice. PHP - Mai multe despre metodele statice. Metode statice și metode nestatice. Metoda statică publică.
Partajeaza in Google Classroom
Memorator: Descriere video Descarcă PDF
O clasă poate avea atât metode statice, cât și nestatice (static and non-static methods). O metodă statică (static method) poate fi accesată dintr-o metodă din aceeași clasă folosind cuvântul cheie self (self keyword) și dublul punct (::):
Exemplu:
<?php
class greeting {
public static function welcome() {
echo "Hello World!";
}
public function __construct() {
self::welcome();
}
}
new greeting();
?>
Metodele statice (static methods) pot fi, de asemenea, apelate la metode din alte clase. Pentru a face acest lucru, metoda statică (static method) ar trebui să fie publică:
Exemplu:
<?php
class greeting {
public static function welcome() {
echo "Hello World!";
}
}
class SomeOtherClass {
public function message() {
greeting::welcome();
}
}
?>