1

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

Glass Robot
  • 2,438
  • 19
  • 11
banditKing
  • 9,405
  • 28
  • 100
  • 157

1 Answers1

3
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
}
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • Thanks that seemed to solve the problem. But my question is How come you say initialize the variable outside the constructor. Isn't it good practice to do so in the constructor? – banditKing Jul 21 '11 at 23:34
  • In short: This _is_ the initialization. Doing so in the constructor is just unnecessary – KingCrunch Jul 21 '11 at 23:39