Last active
August 29, 2015 14:20
-
-
Save harisrozak/e6e682045bc68fcaeab6 to your computer and use it in GitHub Desktop.
PHP :: Static function and variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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