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

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -