2017 © Pedro Peláez
 

library lazy-loading

Quick simple lazy loading trait using reference property and closure

image

jced-artem/lazy-loading

Quick simple lazy loading trait using reference property and closure

  • Thursday, December 15, 2016
  • by jced_artem
  • Repository
  • 1 Watchers
  • 0 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

lazy-loading

Quick simple lazy loading trait using reference property and closure, (*1)

You do not need anymore to write something similar to implement lazy loading:, (*2)

class ObjectFactory
{
    private $object;

    public function getObject()
    {
        if (empty($object)) {
            $this->object = new stdClass();
        }
        return $this->object;
    }
}

And, actually, you also do not need to keep in mind where you need to use empty(), is_null() or $x === false!, (*3)

Just use LazyTrait:, (*4)

class ObjectFactory
{
    use \Jced\LazyTrait;

    private $object1;
    private $object2;
    private $object3;
    private $object4;
    private $object5;

    public function getObject1()
    {
        // assert empty by default
        return $this->lazy($this->object1, new stdClass());
    }

    public function getObject2()
    {
        // assert empty by default
        return $this->lazy($this->object2, function() {
            return new stdClass();
        });
    }

    public function getObject3()
    {
        // assert is_null
        return $this->lazy($this->object3, new stdClass(), false, true);
    }

    public function getObject4()
    {
        // assert is false
        return $this->lazy($this->object4, new stdClass(), false, false, true);
    }

    public function getObject5()
    {
        // assert is false or empty or is_null
        return $this->lazy($this->object5, new stdClass(), true, true, true);
    }
}

Install

composer require jced-artem/lazy-loading, (*5)

Notice

Note that if you pass object as 'return' param - you will get reference. Use new or clone to prevent it if you need., (*6)

The Versions

15/12 2016

dev-master

9999999-dev https://github.com/jced-artem

Quick simple lazy loading trait using reference property and closure

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

by Artem Jced

trait pattern lazy loading

15/12 2016

1.0.1

1.0.1.0 https://github.com/jced-artem

Quick simple lazy loading trait using reference property and closure

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

by Artem Jced

trait pattern lazy loading

01/12 2016

1.0.0

1.0.0.0 https://github.com/jced-artem

Quick simple lazy loading trait using reference property and closure

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Artem Jced

trait pattern lazy loading