php - Laravel - Create method that can be called both statically and non-statically -
i wanted create method in user model behaves differently when called statically or non-statically. should this:
public function foo() { // based on http://stackoverflow.com/questions/3824143/how-can-i-tell-if-a-function-is-being-called-statically-in-php if(isset($this) && get_class($this) == __class__) { $static = true; } else { $static = false; } if($static) $user = auth::user(); else $user = $this; // stuff user return $user->bar; // ... }
but gives me error: non-static method user::foo() should not called statically, assuming $this incompatible context
.
basically:
you can call static methods non-static method not vice versa.
so can use magic methods this:
public function __call($method, $args) { return call_user_func(get_class_name($this) .'::'. $method, $args); }
like this, can call every static method syntax of non-static method.
Comments
Post a Comment