Is there any way in Laravel (5.2) to call static and/or non-static function in a custom object without having to instantiate the referring object in all classes it is used?
Example: I have class App\Helpers\Utilities.php
with public function doBeforeTask()
I'm using this method in allot of classes within my project and it would be pretty if i could just call Utilities::doBeforeTask()
or Utilities->doBeforeTask()
without creating a instance of my Utilities object $obj = new Utilities();
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With