Skip to content

Instantly share code, notes, and snippets.

@harisrozak
Last active August 29, 2015 14:20
Show Gist options
  • Save harisrozak/e6e682045bc68fcaeab6 to your computer and use it in GitHub Desktop.
Save harisrozak/e6e682045bc68fcaeab6 to your computer and use it in GitHub Desktop.
PHP :: Static function and variable
<?php
class Hello
{
private static $greeting = 'Hello, direct static call';
function __construct($string = 'Hello, construct call')
{
self::$greeting = $string;
}
private static function initialize()
{
self::$greeting .= ' is There!';
self::newline();
}
private static function newline()
{
self::$greeting .= '<br />';
}
public static function greet()
{
static $num = 1; // will remember last value
self::initialize();
echo $num++ . '. ' . self::$greeting;
}
public function non_static_function()
{
self::$greeting = 'Overrided! print from non static function';
}
}
Hello::greet(); // Hello There!
$hello = new Hello();
$hello->greet();
$hello->non_static_function();
$hello->greet();
/** Output:
* 1. Hello, direct static call is There!
* 2. Hello, construct call is There!
* 3. Overrided! print from non static function is There!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment