note 109090 added to function.forward-static-call
| From: | jhibbard at gmail dot com | Date: | Tue, 19 Jun 2012 19:55:27 +0000 |
| Subject: | note 109090 added to function.forward-static-call | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188708@lists.php.net to get a copy of this message | ||
Example usage via calls outside of the class and within an object:
<?php
/**
* @author Jonathon Hibbard
*/
class foo {
# used to verify we're actually setting something..
private static $value = '';
/**
* Simple setter for the static method setValue...
*/
public static function set($method_identifier, $value_to_pass = '') {
# make sure we have the right method format...
# another semi-useful example is like this (useful for REST-like requests...):
str_replace(" ", "", ucwords(str_replace("_", " ",
$method_identifier)));
$static_method = 'set' . ucfirst(strtolower(trim($method_identifier)));
if(method_exists(__CLASS__, $static_method)) {
//Note: this will not work and will throw PHP Parse error: syntax error, unexpected
'::'
//__CLASS__::$static_method($value_to_pass);
foo::$static_method($value_to_pass);
echo "\tCalling forward_static_call with pure string and value param:\n";
forward_static_call(__CLASS__ . "::" . $static_method, $value_to_pass);
echo "\tCalling forward_static_call with class, method array and value param:\n";
forward_static_call(array(__CLASS__, $static_method), $value_to_pass);
}
}
/**
* Set self::$value to something?
*/
public static function setValue($value_recieved = '') {
echo "\t\tsetValue called with param of " . var_export($value_recieved, true) .
"!\n";
echo "\t\tSetting Private 'value'...\n";
self::$value = $value_recieved;
echo "\t\tChecking the Private 'value':\n";
if(!empty(self::$value)) {
echo "\t\t\tPrivate 'value' was set to '" . self::$value .
"' as expected!\n";
} else {
echo "\t\t\tPrivate 'value' was not set!\n";
}
# Reset...
self::$value = '';
}
/**
* Create an object and test calling the static method from within this realm...
*/
public function __construct() {
echo "\tCalling from within constructor..\n";
foo::set('value','Something else from within the instance!');
}
}
echo "\n============ Calling by static method first ============\n";
foo::set('value','Something from outside of the foo class!');
echo "\n============ Calling by static method without a value next ============\n";
foo::set('value');
echo "\n============ Calling by createing an instance next ============\n";
new foo();
?>
----
Server IP: 64.71.164.2
Probable Submitter: 69.61.148.242
----
Manual Page -- http://www.php.net/manual/en/function.forward-static-call.php
Edit -- https://master.php.net/note/edit/109090
Del: integrated -- https://master.php.net/note/delete/109090/integrated
Del: useless -- https://master.php.net/note/delete/109090/useless
Del: bad code -- https://master.php.net/note/delete/109090/bad+code
Del: spam -- https://master.php.net/note/delete/109090/spam
Del: non-english -- https://master.php.net/note/delete/109090/non-english
Del: in docs -- https://master.php.net/note/delete/109090/in+docs
Del: other reasons-- https://master.php.net/note/delete/109090
Reject -- https://master.php.net/note/reject/109090
Search -- https://master.php.net/manage/user-notes.php