gpt4 book ai didi

php - 烦人的 PHP 错误 : "Strict Standards: Only variables should be passed by reference in"

转载 作者:可可西里 更新时间:2023-11-01 00:23:08 25 4
gpt4 key购买 nike

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

严格的标准:只有变量应该在 C:\xampp\htdocs\includes\class.IncludeFile.php 中第 34 行通过引用传递!

这是页面:

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 具有以下原型(prototype) 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.

关于php - 烦人的 PHP 错误 : "Strict Standards: Only variables should be passed by reference in",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13117106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com