Php get line called function

Get code line and file that’s executing the current function in PHP?

Question: When I execute the following code, I get output a list of lines from the file, with line numbers. In this case its always , because creates a new index entry for every line Solution 3: A non-associative array automatically gets assigned numeric array keys, i.e. So the array key winds up being the line number minus one.

Get code line and file that’s executing the current function in PHP?

Imagine I have the following situation:

I want to change the log function so that it would produce the following:

So, any way to get the file name and the line number of the code that executed the current function in PHP?

EDIT for backlog usage comments: When I use backlog in my object oriented way of programming I have the following situation.

This example will return as file «Index.php» and as line number 4, this is where the class is initiated. However, it is supposed to return the file TestClass.class.php and line number 6. Any idea how to fix this?

So, in your log function, you would be able to retrieve the filename and line number from which the log function was called.

I’m using this approach in my logging classes and it has significantly reduced the amount of code required to get meaningful log data. Another benefit would be readability. Magic constants tend to get quite ugly when mixed with strings.

Читайте также:  Java new arraylist null

Debug_backtrace() can be used to trace back through the call stack. It can be slow though, so be careful with it if you’re doing a lot of logging.

If you’re using PHP 5.3, you could take advantage of late static binding and have a base class method of log() , and your child classes could call it but still maintain static references to __FILE__ and __LINE__ .

A final option would be just pass __FILE__ and __LINE__ in as parameters when you call your log() function.

This is an old question but seeing as how my solution is not here, I’ll provide it for posterity

 try< throw new Exception(); >catch ( Exception $e )< $trace = $e->getTrace(); > $length = 0; foreach ($trace as $t) < if( $t['file'] != __FILE__ )< break; >++$length; > return array_slice( $trace, ($length - count( $trace ) )); 

You can throw/catch to get a clean stack trace, then you need to look for the first line that contains this file ( typically that is where it is called from ) you can also use the index of the trace if you know it, or the function.

The exception stack trace is pretty much the same as doing debug_backtrace(true) .

PHP — Function XML Get Current Line Number, Syntax. xml_get_current_line_number(parser) ; Definition and Usage. It used to get current line number for an XML parser ; Return Values. It returns the current

Where does the PHP file() function get line numbers?

When I execute the following code, I get output a list of lines from the file, with line numbers.

Now I would imagine the array returned by file() would be a simple one dimensional vector, and I would have to keep a line_num variable in my loop. How do things work here that I get the associative pair $line_num => $line for each line in the file?

file is effectively the same as iterating a filepointer and adding each line in the file to an array. You dont get the line numbers, but the regular numeric index in the array.

function file($filename) < $fp = fopen($filename); while (!eof($fp)) < $lines[] = fgets($fp); >fclose($fp); return $lines; > 

This is of course simplified. If you want to know the exact details, have a look at

foreach ($lines as $line_num => $line)  

foreach (array_expression as $value) statement
foreach (array_expression as $key => $value) statement

The first form loops over the array given by array_expression. On each iteration, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element).

The second form will additionally assign the current element's key to the $key variable on each iteration.

It is not the line number, it is the index of the entry within the array. In this case its always $lineNumber = $index + 1 , because file() creates a new index entry for every line

A non-associative array automatically gets assigned numeric array keys, i.e.

array( 0 => 'first item', 1 => 'second item, . 9 => 'tenth item', ); 

So the array key winds up being the line number minus one.

Now I would imagine the array returned by file() would be a simple one dimensional vector, and I would have to keep a line_num variable in my loop

A PHP array is actually an ordered map:

How to use _LINE_ in PHP to display current line of execution, 2 Answers. Show activity on this post. with two underscores. Look sandbox.onlinephpfunctions.com/code/…, it's work.

How get a current line in file opened with fopen?

I need to get a line position of file pointer from file like below.

string1\n string2\n string3\n 

I'm reading file using this func.

function get() < $fp = fopen('example.txt', 'r'); if (!$fp) < echo 'Error' . PHP_EOL; >while(!feof($fp)) < $string = trim(fgets($fp)); if(!$string) < continue; >else < /* * Here I want to get a line number in this file */ echo $string . PHP_EOL; >> > 

The simple solution is to use a counter.

function get() < // Line number counter $lncount = 0; $fp = fopen('example.txt', 'r'); if (!$fp) < echo 'Error' . PHP_EOL; >while(!feof($fp)) < $string = trim(fgets($fp)); // Increment line counter $lncount++; if(!$string) < continue; >else < // Show line echo "Current line: " . $lncount; echo $string . PHP_EOL; >> > 

Get line and file where object is created in PHP, I solved it by using the function debug_backtrace();

Источник

Оцените статью