簡體   English   中英

煩人的PHP錯誤:“嚴格的標准:只有變量應該通過引用傳遞”

[英]Annoying PHP error: “Strict Standards: Only variables should be passed by reference in”

我有這個小腳本,我不能得到這個錯誤:

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

請幫我。

函數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.
private function Extention()
{
    return end(explode('.', $this->file));
}

end()將指針數組設置為最后一個元素。 在這里,您提供end函數的結果而不是變量。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM