Tylo Tylo - 1 year ago 79
PHP Question

Can I use an instantiated Object as an Array Key?

For example:

$product = new Product("cat");

$sales[$product] = 1;

Answer Source

From the docs:

Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

You could give each instance a unique ID or override __toString() such that it returns something unique and do e.g.

$array[(string) $instance] = 42;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download