-2

I have a confusing little issue.

If I create a simple PHP file:

<?php
    error_reporting (E_STRICT);
    $backtrace = \debug_backtrace();
    $filePath = $backtrace[1]["file"];
?>

I get no error message at all.

If I include it as part of a class :

class Xyz {
    private function traceLog($message) {
        $backtrace = \debug_backtrace();
        $filePath = $backtrace[1]["file"];
        $fileName = \end(\explode("/",$filePath));
        $functionName = $backtrace[1]["function"];
        $lineNumber = $backtrace[1]["line"];
        $this->logger->debug(<removed to keep short>);
    }
}

I get an error message:

PHP Strict Standards: Only variables should be passed by reference on line 27

Where line 27 is :

$fileName = \end(\explode("/",$filePath));

This is using PHP 5.5.27-1+deb.sury.org~precise+1

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
Little Code
  • 1,315
  • 2
  • 16
  • 37

1 Answers1

2

The end function (and all the other array traversing functions) takes its argument by reference. You can't use an expression when a reference is needed, it has to be a variable. You need to do:

$fileArray = \explode("/", $filePath);
$fileName = \end($fileArray);

Of course, you could just use:

$fileName = \basename($filePath);

instead of exploding it.

Barmar
  • 741,623
  • 53
  • 500
  • 612