Sign in
Log inSign up

PHP allows access to non-static methods using ClassName:: But Why does it throw error when accessing non-static properties using ClassName::?

Mohammad Daud Ibrahim's photo
Mohammad Daud Ibrahim
·Aug 7, 2018

Class Names can access non-static methods but cannot access non-static properties

Code

<?php

class BaseClass {
    public $x = 10;
    public function __construct() {
        echo "BaseClass constructor". "<br>";
    }
}

class SubClass extends BaseClass {
    public function __construct() {
        BaseClass::__construct();
        echo "SubClass constructor" . "<br>";
        print parent::$x;
    }
}

(new SubClass());

?>

Run Code to see Result on your Browser