Variable not detected as array

I am trying to create a document register code (Still functions have not been written) and I tried to do an echo to check that my code is working fine. Unfortunately, it is apparently not working, as I get an error of $tag not been an array when it actually is. The error that I get is:

Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\Intranet\Argumentarios\admin\uploader.php on line 37

Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\Intranet\Argumentarios\admin\uploader.php on line 38 Tag

Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\Intranet\Argumentarios\admin\uploader.php on line 37"

The code is:

class Paquete{
    //Variable
    public $tag = array();
    //Funcion
    public function guardar($tag){
        $this->tag = $tag;
    }
    public function grabar($tag){
        //INSERT....
        echo "Tag". $this->tag . "<br/>";
    }
    public function ciclos() {
        for($i = 0; $i < count($this->tag); $i++){
            Paquete::grabar($this->tag[$i]);
        }
    }
}

1 answer

  • answered 2020-08-07 01:05 Barmar

    guardar() should push $tag onto the $this->tag array, not replace the array with a single tag.

    Also, grabar should not echo $this-tag, it should echo the $tag argument. And it should be declared static since it doesn't need to use $this.

    class Paquete{
        //Variable
        public $tag = array();
        //Funcion
        public function guardar($tag){
            $this->tag[] = $tag;
        }
        public static function grabar($tag){
            //INSERT....
            echo "Tag". $tag . "<br/>";
        }
        public function ciclos() {
            foreach ($this->tag as $tag) {
                Paquete::grabar($tag);
            }
        }
    }