Skip to Content
Mastering PHP 7
book

Mastering PHP 7

by Branko Ajzele
June 2017
Intermediate to advanced
536 pages
9h 49m
English
Packt Publishing
Content preview from Mastering PHP 7

The object pool pattern

The object pool pattern manages class instances--objects. It is used in situations where we would like to limit unnecessary class instantiation due to resource-intense operations. The object pool acts much like a registry for objects, from which clients can pick up necessary objects later on.

The following example demonstrates a possible object pool pattern implementation:

<?phpclass ObjectPool{    private $instances = [];    public function load($key)    {        return $this->instances[$key];    }    public function save($object, $key)    {        $this->instances[$key] = $object;    }}class User{    public function hello($name)    {        return 'Hello ' . $name;    }}// Client use$pool = new ObjectPool();$user = new User();$key = spl_object_hash($user);$pool ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning PHP 7

Learning PHP 7

Antonio L Zapata (GBP)
Upgrading to PHP 5

Upgrading to PHP 5

Adam Trachtenberg

Publisher Resources

ISBN: 9781785882814Supplemental Content