PHP array array by reference

  • What is the correct way to use array_splicein PHP? The function header clearly states:

    array_splice ( array &$input , int $offset...therefore it should accept the link as the first argument.

    However string

    array_push(&$this->contextsIds, $contextId);

    Triggers an outdated error : call timed call is outdated on ... line 132

  • How to return an array reference? I have:

    public function &getContextsIds() {
        return is_array($this->contextsIds) ? $this->contextsIds : array();    
    }
    

    but he says note. Only links to links should be returned by link

+3
source share
2 answers
  • The function is already declared to receive the link ( array &$input); You will not need it &when calling a function. Just name it like this:

    array_push($this->contextsIds, $contextId);
    
  • , , . : ? : , array() , - . , , :

    return $this->contextIds;
    
+7

, , :

public function &getContextsIds() {
    return is_array($this->contextsIds) ? $this->contextsIds : array();    
}

, , , , , , , - ..

+1

Source: https://habr.com/ru/post/1779851/


All Articles