Question: For a php project I use a Collection class to handle my objects and lazyloading in Java-like collections.

Now my object has a collection of emailaddresses for example. So I call the getEmailAddresses() function of the object which calls the mapper to return a collection of emailaddresses.

This works fine, but when I do a foreach loop over my collection it returns valid data with the following error in the end:

```
Fatal error: Call to a member function create() on a non-object in /foo/bar/Collection.php on line 89

```

It directs to the following function:

```
public function current()
    {
        if ($this->_collection instanceof Iterator)
            $key = $this->_collection->key();
        else
            $key = key($this->_collection);
        if ($key === null)
            return false;
        $item = $this->_collection[$key];
        if (!is_object($item)) {
            $item = $this->_gateway->create($item);
            $this->_collection[$key] = $item;
        }
        return $item;
    }

```

This line:

```
$item = $this->_gateway->create($item);

```

The \_gateway is the adapter the collection uses. Which I don't use and keep it null. Maybe it has something to do with that?

Anybody has some clue? Because everything is functioning as it should, I can read the collectiondata. It's just the error.

Answer: 
Replace

```
if (!is_object($item))

```

with

```
if (!is_object($item) && !is_null($this->_gateway))

```

This of course only makes sure the code doesn't get called if gateway isn't set, so it doesn't do anything to $item (which might not be what you want).
This only means that `$this->_gateway` is not an object and it should be. It can't be null.

You can change this line:

`$item = $this->_gateway->create($item);`

to

```
if(is_object($this->_gateway)) {
  $item = $this->_gateway->create($item);
}

```

this will fix this error, but can lead to more errors further down, depending on what exactly is `$item` supposed to be.