Skip to content

Instantly share code, notes, and snippets.

@maheshwaghmare
Last active April 26, 2021 15:56
Show Gist options
  • Save maheshwaghmare/53f1e1e82a513c07b32b232f3599c26a to your computer and use it in GitHub Desktop.
Save maheshwaghmare/53f1e1e82a513c07b32b232f3599c26a to your computer and use it in GitHub Desktop.
PHP Class boilerplate - *The Singleton Pattern*
<?php
/**
* MY CLASS NAME
*
* @package MY PACKAGE
* @since 1.0.0
*/
if( ! class_exists( 'MY_CLASS_NAME' ) ) :
/**
* MY_CLASS_NAME
*
* @since 1.0.0
*/
class MY_CLASS_NAME {
/**
* Instance
*
* @access private
* @since 1.0.0
*/
private static $instance;
/**
* Initiator
*
* @since 1.0.0
* @return object initialized object of class.
*/
public static function instance(){
if ( ! isset( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Constructor
*
* @since 1.0.0
*/
public function __construct() {
}
}
/**
* Kicking this off by calling 'instance()' method
*/
MY_CLASS_NAME::instance();
// OR
// $my_class_name = MY_CLASS_NAME::instance();
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment