3

I have this small script made and I cant get this error:

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\includes\class.IncludeFile.php on line 34" off!

Here is the page:

namespace CustoMS;

if (!defined('BASE'))
{
    exit;
}

class IncludeFile
{
    private $file;
    private $rule;

    function __Construct($file)
    {
        $this->file = $file;

        $ext = $this->Extention();
        switch ($ext)
        {
            case 'js':
                $this->rule = '<script type="text/javascript" src="'.$this->file.'"></script>';
                break;

            case 'css':
                $this->rule = '<link type="text/css" rel="stylesheet" href="'.$this->file.'">';
                break;
        }
    }

    private function Extention()
    {
        return end(explode('.', $this->file));
    }

    function __Tostring()
    {
        return $this->rule;
    }
}

Please help me.

hakre
  • 193,403
  • 52
  • 435
  • 836
user1782145
  • 31
  • 1
  • 2
  • Have you examined line 34? Have you checked the documentation about whatever you do on line 34? Do you understand how references work? – lanzz Oct 29 '12 at 07:01
  • possible duplicate of [Strict Standards: Only variables should be passed by reference](http://stackoverflow.com/questions/2354609/strict-standards-only-variables-should-be-passed-by-reference) – Lorenz Meyer Jun 21 '14 at 08:28

2 Answers2

6

function end has following prototype end(&$array).

You can avoid this warning by creating variable and pass it to function.

private function Extention()
{
    $arr = explode('.', $this->file);
    return end($arr);
}

From the documentation:

The following things can be passed by reference:

  • Variables, i.e. foo($a)
  • New statements, i.e. foo(new foobar())
  • References returned from functions, i.e.:

explode returns an array not a reference to array.

For example:

function foo(&$array){
}

function &bar(){
    $myArray = array();
    return $myArray;
}

function test(){
    return array();
}

foo(bar()); //will produce no warning because bar() returns reference to $myArray.
foo(test()); //will arise the same warning as your example.
Community
  • 1
  • 1
Leri
  • 12,367
  • 7
  • 43
  • 60
  • +1 for explaining *why* `end` is throwing the error. It expects to manipulate a reference (`&$array`). – Charles Oct 29 '12 at 07:26
1
private function Extention()
{
    return end(explode('.', $this->file));
}

end() sets the pointer array to the last element. Here you are providing the result of a function to end rather than a variable.

private function Extention()
{
    $array = explode('.', $this->file);
    return end($array);
}
Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261