Back to overview

Find where a method was called in PHP

30 November 2023

Image of Sebastian

Sebastian

Methods that get called in a bunch of different places can be tricky to debug. For example, why is this model being saved?

In PHP, you can find the caller function or method using debug_backtrace().

{ public function save() { var_dump(debug_backtrace()[1]['function']); parent::save(); } }

Let's break this down:

  • debug_backtrace() returns the full backtrace

  • [1] will return the previous location in the backtrace ([0] would be the current location)

  • function will return the function or method at the location

Logging the caller in Ray

If you're using Ray, there's a caller helper to save you some keystrokes and send it straight to Ray.

{ public function save() { ray()->caller(); parent::save(); } }

Running ray()->caller will log the file, class, and method that was responsible for calling a method.

Stay in the loop with updates & promotions for Ray

We only send a few emails a year.

Debug without breaking your flow

Ray keeps all your debug output neatly organized in a dedicated desktop app.

Licenses are valid for 1 year and managed through Spatie. Licenses purchased before Ray 3 remain valid. VAT is calculated at checkout.