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

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

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?
In short: This is the initialization. Doing so in the constructor is just unnecessary

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.