Sumsar
Sumsar

Reputation: 134

Object is NULL after using new

I have this small error i can't get my head around. I kept getting the following error:

Warning: Creating default object from empty value 

And i figured out it is because $newkey is NULL, What i don't understand is why it is null after i assign it?

$newKey = new AccessKey;
$newkey->_token = $data[0]['token'];
$newKey->_id = $data[0]['id'];
$newKey->_workforceid = $data[0]['workforce'];

The class itself looks like this

class AccessKey
{
    private $_id = -1; 
    private $_token = "";
    private $_workforceid = -1;
}

Thanks in advance for any help

Upvotes: 0

Views: 418

Answers (2)

M. Eriksson
M. Eriksson

Reputation: 13635

Issue #1
Check your casing.

You instantiate it as $newKey (capital K) but are using $newkey (small k) later on.

Issue #2
As @JaredFarrish mentioned, you also need to change the properties from private to public if you're going to set the properties outside the class.

Upvotes: 5

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35963

try this:

$newKey = new AccessKey;
$newKey->_token = $data[0]['token'];
$newKey->_id = $data[0]['id'];
$newKey->_workforceid = $data[0]['workforce'];

instead of this

$newKey = new AccessKey;
$newkey->_token = $data[0]['token'];
$newKey->_id = $data[0]['id'];
$newKey->_workforceid = $data[0]['workforce'];

The problem is $newkey is different to $newKey

After if you want to set property class you need to change private to public if you want to set It out of the class

DEMO

Upvotes: 1

Related Questions