Why does PHP allow calling non-static method statically using Class Names and by the various keywords such as self, static & parent which are placeholders for Class Names?
But on the other hand it does not allow calling non-static properties statically?
Here is the sample code -
<?php
# PHP Version 7.1.7
error_reporting(E_ALL);
ini_set('display_errors', 1);
class Fruit {
public $name = 'Fruit';
public function x() {
echo "Fruit_x" . "<br>";
}
}
class Orange extends Fruit {
public $name = 'Orange';
public function x() {
echo "Orange_x" . "<br>";
parent::x();
self::y();
static::z();
// Code Below will throu Uncaught Error: Access to undeclared static property
// echo parent::$name . "<br>";
// echo self::$name . "<br>";
}
public function y(){
echo "Y" . "<br>";
}
public function z(){
echo "Z" . "<br>";
}
}
(new Orange)->x(); // No Warnings
Orange::x(); // However calling like this shows warning as of PHP 5 & 7
// Docs - http://php.net/manual/en/language.oop5.static.php
?>