【问题标题】:PHP Strict Standards: Only variables should be passed by reference error [duplicate]PHP严格标准:只有变量应该通过引用错误[重复]
【发布时间】:2015-09-24 02:40:19
【问题描述】:

我是 php OOP 的新手,我需要一些帮助来修复以下错误。

PHP 严格标准:在第 30 行的 /home/XXX/public_html/h1.com/includes/Article.class.php 中,只能通过引用传递变量

这里是 Article.class.php 的代码 --> loadByUrl

<?php
    class Article extends DBEntity
    {
        static $table= 'article', $pkey = 'articleID', $db, $fields, $fieldnames;

        static $relations= array(

        );

        function save($model = null)
        {
            if(($article = $this->loadByUrl($this->getUrl())) && $article->articleID != $this- >articleID)
            {
                trigger_error("The URL specified for this article is in conflict with another article.");
                return false;
            }

            return parent::save($model);
        }


        public static function loadByUrl($url, $type = null)
        {
        $url = trim($url);

        $conditions = array("articleURL = '$url'");

        if($type)
        $conditions[] = "articleType = '$type'";

        // Line 30 error
        return array_pop(Article::listAll(new DBEListCriteria(implode(' and ', $conditions), null, new DBEListOffset(1))));
        }

        }

    class DBEListCriteria
    {
        private $condition;

        function __construct($condition = '')
        {
            $this->condition = $condition;
        }

        function __toString()
        {
            if(!empty($this->condition))
                return ("where " . $this->condition);
            return "";
        }

        function getCondition()
        {
            return $this->condition;
        }
    }


    class DBEListOffset
    {
        protected $offset, $limit;

        function __construct($limit = 50, $offset = 0)
        {
            $this->offset = intval($offset);
            $this->limit = intval($limit);
        }

        function __toString()
        {
            if(!empty($this->offset) && !empty($this->limit))
                return " limit {$this->offset}, {$this->limit} ";
            else if(empty($this->offset) && !empty($this->limit))
                return " limit {$this->limit} ";
            else
                return "";
        }
    }
?>

Article::loadByUrl called at initialisation time to get an environment variable into $_ENV[site] array:

if($article = Article::loadByUrl(''))
$_ENV[site]->Article = $article;

我正在使用 PHP5.5

【问题讨论】:

    标签: php


    【解决方案1】:

    array_pop() 将数组作为引用,并且您正在传递无法引用的函数的返回值。见Passing By Reference。

    只需更改为:

    $array = Article::listAll(new DBEListCriteria(implode(' and ', $conditions), null, new DBEListOffset(1)));
    return array_pop($array);
    

    但我会问,为什么不将查询中的行ORDER BY 和LIMIT 排序为1?

    【讨论】:

    猜你喜欢
    • 2014-11-01
    • 2015-05-27
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 2014-08-17
    • 2016-10-11
    相关资源
    最近更新 更多