2

I am trying to add a value to the end of an array. I am using:

array_push($this->_attributes["class"],$value);

Now I know that the first parameter has to be an array. Upon inspection :

var_dump($this->_attributes["class"]); die(0);
array_push($this->_attributes["class"],$value);

I can see that the value being passed in is indeed an array as it should be. I am not sure how or why I am getting a string being passed. The output of the var_dump look like such:

array (size=0)
    empty

Why or how is $this->_attributes["class"] being seen as a string and not an array?

Edit: If I invert the two lines like so:

array_push($this->_attributes["class"],$value);
var_dump($this->_attributes["class"]); die(0);

The var_dump looks like this:

array (size=1)
   0 => string 'btn' (length=3)

This is the expected output. If I remove the var_dump, I get a fatal error on the array_push again.

** Full Class Declaration** This is enough of the class I able building for this example:

class Tag
{
    protected $_attributes = array("class"=>array());

    public function setAttribute($attribute,$value)
    {
        if( $attribute === "class" ) {
            $this->setClassAttribute($value);
        }
        $this->_attributes[$attribute] = $value;
    }
    public function setClassAttribute($value)
    {
        if( is_array($value) ) {
            foreach ($value as $c) {
                $this->setClassAttribute($c);
            }
            return;
        }
        // var_dump($this->_attributes["class"]); die(0);
        array_push($this->_attributes["class"],$value);
        // var_dump($this->_attributes["class"]); die(0);
    }
}

To execute it:

$tag = new Tag();
$tag->setAttribute("class","btn");
5
  • 2
    Please provide a minimal piece of code with which we can reproduce this. Definition of $this->_attributes? Commented Dec 10, 2016 at 23:50
  • Your updated sample doesn't reproduce the issue. - edit: looks like you have to set twice to cause. Commented Dec 11, 2016 at 0:03
  • Try using $this->_attributes["class"][] = $value instead. It might work, and if not, then you will likely get a more descriptive error. Commented Dec 11, 2016 at 0:03
  • @HPierce I am using xdebug. But this is not the case. If I were to var_dump($this->_attributes) then I would get more of a result like you are talking about. I will add it in the question so it is more legible. Commented Dec 11, 2016 at 0:04
  • @SeinopSys $this->_attributes["class"][] = $value throw a [] operator not supported for strings. Which is essentially the same problem. Commented Dec 11, 2016 at 0:06

1 Answer 1

1

Your problem is here:

public function setAttribute($attribute,$value)
{
    if( $attribute === "class" ) {
        $this->setClassAttribute($value);
    }
    $this->_attributes[$attribute] = $value;
}

setClassAttribute is indeed setting the value to array("btn"). Afterwards, it's being overwritten by the line outside the statement. Try:

public function setAttribute($attribute,$value)
{
    if( $attribute === "class" ) {
        $this->setClassAttribute($value);
    } else {
        $this->_attributes[$attribute] = $value;
    }
}


REPL:

php > $tag = new Tag();
php > $tag->setAttribute("class","btn");
array(0) {
}
array(1) {
  [0]=>
  string(3) "btn"
}
php > $tag->setAttribute("class","btn");
string(3) "btn"

Warning: array_push() expects parameter 1 to be array, string given in php shell code on line 21
string(3) "btn"
Sign up to request clarification or add additional context in comments.

1 Comment

After adding the else clause, the error goes away. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.