static variable not accessible via an object or $this but static functions
are accessible via $this or object in PHP
<?php
class c1
{
public static function f1()
{
return "hello";
}
public static $a=10;
public function f2()
{
echo $this->f1(); //prints "hello"
echo $this->a;//ERROR:Undefined property: c1::$a in
C:\wamp\www\class_in_php\example5.php on line 14
}
}
$obj1=new c1;
$obj1->f2();
?>
Why can't we access a static variable of a class using $this or an object
of that class??? But we can access a static function of that class using
$this or an object of that class.
What is the reason behind such a phenomenon?
No comments:
Post a Comment