Sobes.tech
Senior

class Cache { private static $data = []; public function set($key, $value) { self::$data[$key] = $value; } public function get($key) { return self::$data[$key] ?? null; } } $cacheA = new Cache(); $cacheA->set('user', 'Alice'); $cacheB = new Cache(); echo $cacheB->get('user'); // ?

sobes.tech AI

Answer from AI

In this code, the $data property is declared as static, meaning it is shared among all instances of the Cache class.

When $cacheA->set('user', 'Alice'); is called, the value 'Alice' is stored in the static array $data under the key 'user'.

When $cacheB->get('user'); is called — even though it is a different object, it accesses the same static array, so it will return 'Alice'.

Conclusion:

Alice

This demonstrates that static properties belong to the class, not to a specific object.