I am looking to obtain on which line a method was executed in real time for educational purposes.
For example given the following code:
<?php
class FooClass
{
public function testFunction($fakeArgument)
{
//obtener de que archivo y linea de ese archivo fue ejecutado
//output: test.php linea: 12
}
}
$fooClass = new FooClass();
$fooClass->testFunction('Hola Mundo');
Example of the view in the editor:
I have tried with this:
$reflector = new \ReflectionClass('FooClass');
echo $reflector->getFileName();
But this only returns the file where the class is defined; I seek to know the file from which it is invoked ;
How can I get the file and the line where the method is invoked?
There is no native method ready to use, but there is a method that can give us some of the information
debug_backtrace()
:Implementation:
A. You must use it within the method or execution chain of the methods to be able to obtain what is being executed so far, for example:
we will get an output like this:
This is an array with the two methods that were executed in sequence where we will have as the first element; the last method and so on.
Within the array we will find the elements by method
file
andline
that are the question I had.The implementation would already depend on each developer, but with this navigable array the main thing that was sought is obtained.