PHP code example of iiifx-production / lazy-init
1. Go to this page and download the library: Download iiifx-production/lazy-init library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
iiifx-production / lazy-init example snippets php
class DeepThought
{
protected $answer;
public function getAnswer ()
{
if ( $this->answer === null ) {
$this->answer = 42;
}
return $this->answer;
}
}
$deepThought = new DeepThought();
echo $deepThought->getAnswer(); # 42
php
class DeepThought
{
use \iiifx\LazyInit\LazyInitTrait;
public function getAnswer ()
{
return $this->lazyInit( function () {
return 42;
} );
}
}
$deepThought = new DeepThought();
echo $deepThought->getAnswer(); # 42
php
mixed lazyInit( Closure $container, string|array $dependency = null, array $params = [] )
php
mixed lazyInitStatic( Closure $container, string|array $dependency = null, array $params = [] )
php
class Lazy
{
use \iiifx\LazyInit\LazyInitTrait;
/**
* @return string
*/
public function getDate ()
{
return $this->lazyInit( function () {
return date( 'd.m.Y' );
}, __METHOD__ );
}
}
$lazy = new Lazy();
echo $lazy->getDate(); # '12.07.2015'
php
class Lazy
{
use \iiifx\LazyInit\LazyInitTrait;
/**
* @return string
*/
public function getMicrotime ()
{
return $this->lazyInit( function () {
return microtime( true );
} );
}
}
$lazy = new Lazy();
echo $lazy->getMicrotime(); # 1438928722.9734
php
class Lazy
{
use \iiifx\LazyInit\LazyInitTrait;
/**
* @param string $string
*
* @return mixed[]
*/
public function parseString ( $string )
{
return $this->lazyInit( function () use ( $string ) { # Передаем параметр в замыкание напрямую
return explode( ':', $string );
}, [
__METHOD__,
$string,
] );
}
/**
* @param int $timastamp
*
* @return string
*/
public function formatTimastamp( $timastamp )
{
return $this->lazyInit( function ( $t ) {
return date( 'd.m.Y', $t );
}, [
__METHOD__,
$timastamp,
], [
$timastamp # Передаем параметр как свойство
] );
}
}
$lazy = new Lazy();
var_export( $lazy->parseString( 'A:B:C' ) ); # [ 0 => 'A', 1 => 'B', 2 => 'C' ]
var_export( $lazy->formatTimastamp( time() ) ); # '12.07.2015'
php
class LazyStatic
{
use \iiifx\LazyInit\LazyInitStaticTrait;
/**
* @return string
*/
public static function getDate ()
{
return self::lazyInitStatic( function () {
return date( 'd.m.Y' );
}, __METHOD__ );
}
}
echo LazyStatic::getDate(); # '12.07.2015'
php
use iiifx\LazyInit\LazyInitHelper;
function buildString( $array )
{
return LazyInitHelper::lazyInit( function ($v) {
return implode( '.', $v );
}, 'build-string', [ $array ] );
}
echo buildString( [ 1, 5, 32 ] ); # '1.5.32'
php
class Multiton
{
use \iiifx\LazyInit\LazyInitStaticTrait;
private function __clone () {}
private function __wakeup () {}
public $key;
protected function __construct ( $key )
{
$this->key = $key;
}
/**
* @param string $key
*
* @return static
*/
public static function getInstance ( $key )
{
return static::lazyInitStatic( function ( $key ) {
return new static( $key );
}, $key, [ $key ] );
}
}
echo Multiton::getInstance( 'master' )->key; # 'master'
echo Multiton::getInstance( 'slave' )->key; # 'slave'
echo Multiton::getInstance( 'master' )->key; # 'master'