-1

I am new to php OOP and I need some help fixing the following error.

PHP Strict Standards: Only variables should be passed by reference in /home/XXX/public_html/h1.com/includes/Article.class.php on line 30

Here is the code of 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;

I am using PHP5.5

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
new2phpoop
  • 11
  • 2

1 Answers1

2

array_pop() takes an array as a reference and you are passing the return of a function which can't be referenced. See Passing By Reference.

Simply change to:

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

But I would ask, why not sort ORDER BY the rows in the query and LIMIT to 1?

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • 2
    Maybe you also want to add the reference: http://php.net/manual/en/language.references.pass.php to the list what can be passed by reference. (So this question would get a nice bookmark for similar questions :) – Rizier123 Jul 06 '15 at 20:02