Search code examples
phparray-push

Getting error message for array_push method in php


Here is the code for my class:

<?php
include ('Special.php');

class SpecialContainer
{
    private $dataArray;

    public function _construct()
    {
       $this->dataArray = array();
        echo"Created new Location instance<br/>";
    }

    public function addSpecialItem($Special_Item)
    {
        array_push($this->dataArray, $Special_Item);
    }

}
?>

Its throwing an error at the following line in another php file:

$SpecialContainerObj->addSpecialItem($SpecialObj);

The error is this:

Warning: array_push() [function.array-push]: First argument should be an array in /home/**********s/SpecialContainer.php on line 16

..

Im confused, could someone clarify please how I can resolve this. Thanks


Solution

  • public function _construct()
    

    There is a missing underscore. You should also notice, that your message is never echod

    public function __construct()
    

    However, you should define something like this directly within the class declaration

    class Foo {
      private $dataArray = array();
    
      // Other code
    }