Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annoying PHP error: "Strict Standards: Only variables should be passed by reference in"

Tags:

php

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.

like image 482
user1782145 Avatar asked Nov 21 '25 01:11

user1782145


1 Answers

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.
like image 71
Leri Avatar answered Nov 23 '25 17:11

Leri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!