Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chrisgrim's avatar

Using Static functions bad practice?

Hi, I am trying to clean my controller and I am running into an issue. My model event.php is getting really long and I am hesitant to move more functions to it. So I created a new model called MakeImage to handle images. This allows me to move the logic out of the controller but not overwhelm my event model. It has also allowed me to reuse the same code for organizers and users. However I read everywhere that calling static functions is bad practice.

What should I be doing? Here is an example of my controller updating my event image.

/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Event $event)
    {
        $event->inProgress() ? MakeImage::saveNewImage($request, $event, 1280, 720, 'event') : MakeImage::updateImage($request, $event, 1280, 720, 'event');
        $event->updateEventStatus(8, $request);
        return $event;

    }
0 likes
12 replies
martinbean's avatar

@chrisgrim I don’t really know what your MakeImage class is doing but yes, static methods are usually frowned upon as now you can’t mock MakeImage::saveNewImage or MakeImage::updateImage if you were to test that controller action.

Saving an image against a model isn’t really that much trouble:

if ($request->hasFile('image')) {
    $event->image_path = $request->file('image')->store('event-images');
}
sr57's avatar

However I read everywhere that calling static functions is bad practice.

Where do you read this?

  • Laravel is full of static functions

  • static function are faster

sr57's avatar

HI @tray2

Yes, of course Laravel is also full of 'instance of class'

I just answer to the question, static function is not a bad practice, it depends of the usage (like everything)

martinbean's avatar

Static functions should only be used where the method is stateless.

1 like
Tray2's avatar

Agreed.

Like when you need the current time and don't care about anything else.

$timestamp = Carbon::now();
martinbean's avatar

Facades use static methods, but only to get things out of a container that then don’t use static methods, including the database-related components in the framework. Open up any file in the Illuminate/Database namespace and you’ll see they’re primarily non-static methods.

Facades are not an example of coding with statics.

sr57's avatar

Yes, and you wrote

Facades use static methods, ...

So same answer to the question "static function is not a bad practice"

martinbean's avatar

Well done for picking only part of a quote and completely disregarding the caveat straight after.

3 likes
jlrdw's avatar
jlrdw
Best Answer
Level 75

@sr57 facades use __callStatic().

There is nothing wrong with static methods like in helper functions.

The best way to learn how Taylor does things in laravel, is to study the Frameworks Vendor code.

As an example string helpers in Str.php, @taylorotwell has static methods:

See:

https://github.com/laravel/framework/blob/ff5e8c55100fd416ec0c0ff23db583a61ce7a3fb/src/Illuminate/Support/Str.php

So if doing a helper that always does the exact same thing to an image upload, perhaps a resize, then instance or static, wouldn't make much difference.

Static methods are good when doing a certain thing that may be needed in various class methods.

Like part of a string being replaced can be used anywhere needed.

Are they bad practice, no.

2 likes
orest's avatar

Jeffrey in one of his videos does the following

Activity::feed($user);

Where Activity is a model and the method feed returns the activities for the given user.

Is this a bad use case for static methods ?

Please or to participate in this conversation.