tutorial_lazy_initialization.php 914 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. require_once 'tutorial_autoload.php';
  3. // Create a custom class implementing the singleton pattern
  4. class customSingleton
  5. {
  6. protected static $instance;
  7. public static function getInstance()
  8. {
  9. if ( self::$instance === null )
  10. {
  11. self::$instance = new customSingleton();
  12. ezcBaseInit::fetchConfig( 'customKey', self::$instance );
  13. }
  14. return self::$instance;
  15. }
  16. }
  17. // Implement your configuration class
  18. class customSingletonConfiguration implements ezcBaseConfigurationInitializer
  19. {
  20. public static function configureObject( $object )
  21. {
  22. echo "Configure customSingleton.\n";
  23. $object->value = 42;
  24. }
  25. }
  26. // Register for lazy initilization
  27. ezcBaseInit::setCallback( 'customKey', 'customSingletonConfiguration' );
  28. // Configure on first initilization
  29. $object = customSingleton::getInstance();
  30. var_dump( $object->value );
  31. ?>