Static Properties Inside a Class in OOP in PHP
You can access static properties
inside the class itself using self::.
See the example:
<?php
class Test
{
// Private static property:
private static $property;
// Static method for setting the property value:
public static function setProperty($value)
{
self::$property = $value; // write data to our static property
}
// Static method for getting the property value:
public static function getProperty()
{
return self::$property; // read the recorded data
}
}
?>
Let's use our class:
<?php
Test::setProperty('test'); // write data to the property
echo Test::getProperty(); // output to the screen
?>
Create a class Num that will have
two private static properties: num1
and num2. Let the property
num1 store the number 2 by default, and the
property num2 store the number 3.
Create a method getSum in the class Num,
which will output the sum of the values
of the properties num1 and num2 to the screen.