3v4l.org

run code in 300+ PHP versions simultaneously
<?php class User{ protected $activeProducts = null; protected $pdo; public function __construct(PDO $pdo = null){ $this->pdo = $pdo; } public function getProducts(){ if($this->activeProducts){ echo "getting from property (cache)\n"; return $this->activeProducts; } //$this->activeProducts = $this->pdo... a long task echo "getting from db\n"; $this->activeProducts = ['fake','products']; return $this->activeProducts; } } $user = new User(null);//for tests var_dump($user->getProducts()); var_dump($user->getProducts());

preferences:
67.68 ms | 402 KiB | 5 Q