Class: Static Property &  Method

Objectives

  • distinction between static properties (variable) and static method

Code

<?php
class base
{
public static $prop = "--- A string data";

public function base()
{
echo "<br/>constructor -- evoked";
}
public static function something($data)
{
echo "<br/>static base something <font style='color:red'>". $data."</font>";
}

public static function test()
{
base::something("Hello");
}
public function test1()
{
base::something(" Called from non static function");
}
}
base::something("Hello World");
$bstatic = new Base();
$bstatic->something("can i do that! oops");
base::test();
base::test1();
echo "<br/>accessing public static variable";
echo base:: $prop;
// will throw run time error
$bstatic->$prop;

?>

Runtime View