10

Is there any way in Laravel (5.2) to call static and non-static functions in a custom object without instantiating the referring object in all classes used?

Example: I have class App\Helpers\Utilities.php with public function doBeforeTask()

I'm using this method in a lot of classes within my project, and it would be pretty if I could call Utilities::doBeforeTask() or Utilities->doBeforeTask() without creating an instance of my Utilities object $obj = new Utilities();

1
  • 3
    Wouldn't renaming it to public static function doBeforeTask() accomplish exactly what you need?
    – apokryfos
    Commented Jul 15, 2016 at 8:18

6 Answers 6

20

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.
 }
}
6

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
2

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"); 
0
2

Laravel also has a Facade implementation, which is probably what TS had in mind. These Facades will do basically everything for you and most likely also solves the "vendor-file" issue.

https://www.larashout.com/creating-custom-facades-in-laravel

basically you have to provide an instance of it and point your facade to it, which in turn get's an alias you register. everything is explained in above url.

1

Define static function

class Foo
{

    public static function staticFunction() {
        return 'Hello World';
    }
}

now call Foo::staticFunction()

4
  • But does it not require some sort of autoload or implementation in composer.json such as service provider or alias?
    – boomdrak
    Commented Jul 15, 2016 at 8:33
  • 1
    If you want to use it everywhere you can either require it in the autoload or add it as dep in composer
    – Frnak
    Commented Jul 15, 2016 at 8:37
  • Not sure if you're supposed to create it but there is no app/helpers folder in Laravel 6.
    – NoBugs
    Commented Nov 27, 2019 at 4:16
  • @boomdrak, no not in this case, because it's simple plain PHP implementation. All that is required for autoloading is ofcourse the psr-4 compliant namespace. What you are thinking of is Laravel's Facade.
    – SomeOne_1
    Commented Aug 20, 2020 at 13:40
1

For dynamic static calls

Utilities::anyMethod($args);

PHP code

<?php
namespace App\Library;

class Utilities {
  public function anyMethod() {
     // simple code
  }

  public static function __callStatic($method, $args)
  {
      $instance = static::resolveFacadeInstance(static::getFacadeAccessor());
      switch (count($args))
      {
        case 0:
            return $instance->$method();

        case 1:
            return $instance->$method($args[0]);

        case 2:
            return $instance->$method($args[0], $args[1]);

        case 3:
            return $instance->$method($args[0], $args[1], $args[2]);

        case 4:
            return $instance->$method($args[0], $args[1], $args[2], $args[3]);

        default:
            return call_user_func_array(array($instance, $method), $args);
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.