PHPackages                             yarri/lazy-loader - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yarri/lazy-loader

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yarri/lazy-loader
=================

LazyLoader provides efficient mechanism for lazy loading with closures

v2.0(4y ago)026.0k↓40.7%MITPHPPHP &gt;=7.1

Since May 14Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/yarri/LazyLoader)[ Packagist](https://packagist.org/packages/yarri/lazy-loader)[ Docs](https://github.com/yarri/LazyLoader)[ RSS](/packages/yarri-lazy-loader/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (3)Versions (8)Used By (0)

LazyLoader
==========

[](#lazyloader)

[![Build Status](https://camo.githubusercontent.com/66046b71def99e7ef854a10b6b8c091b18bb87ade122f1edd0aef94544418661/68747470733a2f2f6170702e7472617669732d63692e636f6d2f79617272692f4c617a794c6f616465722e7376673f6272616e63683d6d6173746572)](https://app.travis-ci.com/yarri/LazyLoader)

LazyLoader provides efficient mechanism for lazy loading with closures.

In LazyLoader one or more named closures can be defined. Any closure is called at most once when its output is needed.

Basic Usage
-----------

[](#basic-usage)

LazyLoader implements ArrayAccess, thus the easiest way how to use is like an associative array.

```
$lazy_loader = new LazyLoader();

// Setting up a closure,
$lazy_loader["recent_articles"] = function(){
   return Article::FindAll(["order_by" => "published_at DESC", "limit" => 10]);
};
// ... another closure
$lazy_loader["top_product"] = function(){
  return Product::FindFirst(["order_by" => "pieces_sold DESC"]);
}

// Reading - closure is being executed only during the first occurence of reading
if($lazy_loader["recent_articles"]){
  foreach($lazy_loader["recent_articles"] as $article){
    // ...
  }
}
//
$top_product = $lazy_loader["top_product"];

```

There are total three ways how to set a closure or get its output. All of them do the same thing and can be mixed.

```
// Setting a closure
$lazy_loader->set("recent_articles",function(){ /* ... */ });
// or
$lazy_loader["recent_articles"] = function(){ /* ... */ };
// or
$lazy_loader->setRecentArticles(function(){ /* ... */ });

// Getting the output
$recent_articles = $lazy_loader->get("recent_articles");
// or
$recent_articles = $lazy_loader["recent_articles"];
// or
$recent_articles = $lazy_loader->getRecentArticles();

```

Also arguments can be involved. In this case the usage of camelized virtual methods comes in handy.

```
$lazy_loader->setRecentArticles(function($limit = 10){
   return Article::FindAll(["order_by" => "published_at DESC", "limit" => $limit]);
});

$five_recent_articles = $lazy_loader->getRecentArticles(5);

```

### Usage in a template engine

[](#usage-in-a-template-engine)

LazyLoader can be gracefully used in any template engine, for instance in [the Smarty](http://www.smarty.net/).

Preparing data for a Smarty template:

```
$smarty->assign("lazy_loader",$lazy_loader);

```

In a Smarty template:

```
Recent Articles

  {foreach $lazy_loader.recent_articles as $article}
    {$article->getTitle()}
  {/foreach}

```

### Usage in the ATK14 Framework

[](#usage-in-the-atk14-framework)

```
