Accessing Static Through Class and Object in OOP in PHP
Static properties and methods can be accessed both through the class, and through a variable with an object of the class.
Let's look at an example.
Let's say we have a class Test
with a static property:
<?php
class Test
{
public static $property = 'static';
}
?>
Let's output the value of the static property, by accessing the class:
<?php
echo Test::$property;
?>
And now the value of the static property, by accessing the object of the class:
<?php
$test = new Test;
echo $test::$property;
?>
The following class with a static method is given:
<?php
class Test
{
public static function show()
{
return '+++';
}
}
?>
Call this method as a class method, and as an object method.