Last active
May 18, 2022 03:03
-
-
Save nathanbrauer/cdd286351f68a1b4e3a5 to your computer and use it in GitHub Desktop.
Ways to alias a method on a class.
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 | |
/** | |
* Ways to alias a method on a class. | |
* | |
* Methods are in order of least preferred (top) to most preferred (bottom). Code style, not performance, is taken into account. | |
*/ | |
Class Program { | |
const SECONDS = 1; | |
const MICROSECONDS = 2; | |
public function wait($length,$type=self::SECONDS) { | |
//This does something... | |
} | |
/** | |
* Implement an alias via the __call override | |
*/ | |
public function __call($name, $arguments) { | |
if ($name === 'rest') { | |
return call_user_func_array(array($this,'wait'),$arguments); | |
} | |
//etc | |
} | |
/** | |
* Alias of wait, hardcoded | |
*/ | |
public function sleep($seconds,$type=self::SECONDS) { | |
return $this->wait($seconds,$type); | |
} | |
/** | |
* Alias of wait using variable parameters (Before 5.6) | |
*/ | |
public function nap() { | |
return call_user_func_array(array($this,'wait'),func_get_args()); | |
} | |
/** | |
* Alias of wait using PHP 5.6 variadic functions | |
* http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list | |
*/ | |
public function hold(...$params) { | |
return $this->wait(...$params); | |
} | |
} |
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 | |
/** | |
* Ways to alias a STATIC method on a class. | |
* | |
* Methods are in order of least preferred (top) to most preferred (bottom). Code style, not performance, is taken into account. | |
*/ | |
Class Program { | |
const SECONDS = 1; | |
const MICROSECONDS = 2; | |
public static function wait($length,$type=self::SECONDS) { | |
//This does something... | |
} | |
/** | |
* Implement an alias via the __call override | |
*/ | |
public static function __callStatic($name, $arguments) { | |
if ($name === 'rest') { | |
return call_user_func_array(array(get_called_class(),'wait'),$arguments); | |
} | |
//etc | |
} | |
/** | |
* Alias of wait, hardcoded | |
*/ | |
public static function sleep($seconds,$type=self::SECONDS) { | |
return static::wait($seconds,$type); | |
} | |
/** | |
* Alias of wait using variable parameters (Before 5.6) | |
*/ | |
public static function nap() { | |
return call_user_func_array(array(get_called_class(),'wait'),func_get_args()); | |
} | |
/** | |
* Alias of wait using PHP 5.6 variadic functions | |
* http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list | |
*/ | |
public static function hold(...$params) { | |
return static::wait(...$params); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment