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? 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.
 }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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.

Comments

1

Define static function

class Foo
{

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

now call Foo::staticFunction()

4 Comments

But does it not require some sort of autoload or implementation in composer.json such as service provider or alias?
If you want to use it everywhere you can either require it in the autoload or add it as dep in composer
Not sure if you're supposed to create it but there is no app/helpers folder in Laravel 6.
@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.
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);
      }
    }
  }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.