Laravel - How to call static function without instantiate object
define your method as static method. and call it anywhere with following code:
Utilities::doBeforeTask();
Code structure of file App\Helpers\Utilities.php
namespace App\Library;
class Utilities {
//added new user
public static function doBeforeTask() {
// ... you business logic.
}
}
Define your method as a static method. and call it anywhere
let's take an example
namespace App\Http\Utility;
class ClassName{
public static function methodName(){
// ... you business logic.
}
}
where you want to use specify the namespace
like this:
use App\Http\Utility\ClassName;
ClassName::methodName();
Don't forget to run
composer dump-autoload
If it's a method that you cannot change to static (i.e. it's a vendor file) then you can do this in PHP >= 5.4
$something = (new Something)->foo("bar");