3

我制作了这个小脚本,但无法收到此错误:

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

这是页面:

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;
    }
}

请帮我。

4

2 回答 2

6

函数end有以下原型end(&$array)

您可以通过创建变量并将其传递给函数来避免此警告。

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

从文档中:

可以通过引用传递以下内容:

  • 变量,即 foo($a)
  • 新语句,即 foo(new foobar())
  • 从函数返回的引用,即:

explode返回一个数组而不是对数组的引用。

例如:

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.
于 2012-10-29T07:07:32.827 回答
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);
}
于 2012-10-29T07:09:04.013 回答