2012-10-29 32 views
3

我有这个小脚本量身定做的,我不能让这个错误:恼人的PHP错误:“严格的标准:只有变量应参考在传递”

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

哪一条是第34行? – Leri

+0

您是否检查了第34行?你是否检查过关于第34行的任何文档?你知道参考文献的工作原理吗? – lanzz

+0

[严格标准:只有变量应通过引用传递]的可能重复(http://stackoverflow.com/questions/2354609/strict-standards-only-variables-should-be-passed-by-reference) –

回答

6

功能end具有以下原型end(&$array)

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

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

从文档:

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返回一个数组不数组的引用。

例如:

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. 
+0

+1用于解释*为什么*'end'抛出错误。它期望操作一个引用('&$ array')。 – Charles

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

端()设置指针数组的最后一个元素。在这里,您将函数的结果提供给end而不是变量。

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