PHP code example of carloswph / tonton

1. Go to this page and download the library: Download carloswph/tonton 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/ */

    

carloswph / tonton example snippets




class EasyPeasy {

	use \TonTon\Singleton;

	protected function construct() {

		echo 'Easy Peasy!';
	}
}

/* So, if we want to instantiate this class, then we need to use a
   static method. Any new attempts of instantiating the class will
   result in nothing (if we try to use the static method again) or 
   a fatal error, if we try to create an instance through the "new"
   keyword.*/

$ep = EasyPeasy::instance(); // Valid instance
$rt = EasyPeasy::instance(); // Nothing happens, as an instance already exists

$er = new EasyPeasy(); // Fatal error





class EasyPeasy {

	use \TonTon\Multiton;

	protected function construct() {

		echo 'Easy Peasy!';
	}
}

/* So, if we want to instantiate this class, then we need to use a
   static method. Any new attempts of instantiating the class will
   work if using the static method instance(), with a $key as argument
   or a fatal error, if we try to create an instance through the "new"
   keyword.*/

$ep = EasyPeasy::instance('normal'); // Instance 1
$rt = EasyPeasy::instance('log'); // Instance 2

$er = new EasyPeasy(); // Fatal error





class EasyPeasy {

	use \TonTon\Limiton;

	protected function construct() {

		$this->setLimit(1); // Limit set to 1, so it works like a Singleton,
							// but needs a $key for the new instance. Default
							// value is 2.
		echo 'Easy Peasy!';
	}
}

/* So, if we want to instantiate this class, then we need to use a
   static method. Any new attempts of instantiating the class will
   work if using the static method instance(), with a $key as argument
   or a fatal error, if we try to create an instance through the "new"
   keyword. However, in the Limiton, the number of instances is limited
   by the method setLimit(int).*/

$ep = EasyPeasy::instance('normal'); // Instance 1
$rt = EasyPeasy::instance('log'); // Nothing happens, as we limited the max number
								  // of instances in 1 for this class.

$er = new EasyPeasy(); // Fatal error





class EasyPeasy {

	use \TonTon\Cacheton;

	protected function construct() {

		echo 'Easy Peasy!';
	}
}

/* The Cacheton works like a Multiton. Each new instance receives a tag.
   If no time in seconds is passed, the trait assumes you want the instance
   to be cached for one hour. However, this time can be set as a second
   argument while calling the static method.*/

$ep = EasyPeasy::instance('normal'); // Instantiate the class and sets a cache for 3600 seconds.
$rt = EasyPeasy::instance('timed', 60); // Instantiate the class and sets a cache for 60 seconds

$er = new EasyPeasy(); // Fatal error





class EasyPeasy {

	use \TonTon\Selecton;

	private function __construct() {

		echo 'Easy Peasy!';
	}

}

$constraints = [
	'extensions' => [ // Extension names
		'curl',
		'mbstring'
	],
	'classes' => [ // Class names (with namespaces, if existent)
		'Directory',
		'IteratorIterator'
	]
];

$rt = EasyPeasy::instance($constraints); // If any of the constraints is not met, throws an exception

$er = new EasyPeasy(); // Fatal error