令人讨厌的PHP错误:“严格标准:仅变量应通过引用传递给". [英] Annoying PHP error: "Strict Standards: Only variables should be passed by reference in"

查看:106
本文介绍了令人讨厌的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!

这是页面:

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).

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

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

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

从文档中:

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

The following things can be passed by reference:

  • 变量,即foo($ a)
  • 新语句,即foo(new foobar())
  • 从函数返回的引用,即:
  • Variables, i.e. foo($a)
  • New statements, i.e. foo(new foobar())
  • References returned from functions, i.e.:

explode返回一个数组,而不是对该数组的引用.

explode returns an array not a reference to array.

例如:

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.

这篇关于令人讨厌的PHP错误:“严格标准:仅变量应通过引用传递给".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆