No Description

SingletonTrait.php 863B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Singleton class trait.
  4. *
  5. * @package WooCommerce\Utilities
  6. */
  7. namespace Automattic\WooCommerce\RestApi\Utilities;
  8. /**
  9. * Singleton trait.
  10. */
  11. trait SingletonTrait {
  12. /**
  13. * The single instance of the class.
  14. *
  15. * @var object
  16. */
  17. protected static $instance = null;
  18. /**
  19. * Constructor
  20. *
  21. * @return void
  22. */
  23. protected function __construct() {}
  24. /**
  25. * Get class instance.
  26. *
  27. * @return object Instance.
  28. */
  29. final public static function instance() {
  30. if ( null === static::$instance ) {
  31. static::$instance = new static();
  32. }
  33. return static::$instance;
  34. }
  35. /**
  36. * Prevent cloning.
  37. */
  38. private function __clone() {}
  39. /**
  40. * Prevent unserializing.
  41. */
  42. final public function __wakeup() {
  43. wc_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'woocommerce' ), '4.6' );
  44. die();
  45. }
  46. }