Is it possible to declare a method static and nonstatic in PHP?
You can do this, but it's a bit tricky. You have to do it with overloading: the __call
and __callStatic
magic methods.
class test {
private $text;
public static function instance() {
return new test();
}
public function setText($text) {
$this->text = $text;
return $this;
}
public function sendObject() {
self::send($this->text);
}
public static function sendText($text) {
// send something
}
public function __call($name, $arguments) {
if ($name === 'send') {
call_user_func(array($this, 'sendObject'));
}
}
public static function __callStatic($name, $arguments) {
if ($name === 'send') {
call_user_func(array('test', 'sendText'), $arguments[0]);
}
}
}
This isn't an ideal solution, as it makes your code harder to follow, but it will work, provided you have PHP >= 5.3.
I would make a hidden class as the constructor and return that hidden class inside the parent class that has static methods equal to the hidden class methods:
// Parent class
class Hook {
protected static $hooks = [];
public function __construct() {
return new __Hook();
}
public static function on($event, $fn) {
self::$hooks[$event][] = $fn;
}
}
// Hidden class
class __Hook {
protected $hooks = [];
public function on($event, $fn) {
$this->hooks[$event][] = $fn;
}
}
To call it statically:
Hook::on("click", function() {});
To call it dynamically:
$hook = new Hook;
$hook->on("click", function() {});