PHP code example of pozitronik / yii2-cached-properties-trait

1. Go to this page and download the library: Download pozitronik/yii2-cached-properties-trait 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/ */

    

pozitronik / yii2-cached-properties-trait example snippets


/**
 * @property ?int AnswerToUltimateQuestionOfLifeUniverseAndEverything
 */
class DeepThought extends \yii\base\Model {

	/**
	 * Warning: this method execution time take ~7.5 million years.
	 * @return int|null
	 */
	public function getAnswerToUltimateQuestionOfLifeUniverseAndEverything():?int {
		return $this->multiple(6, 9);//42
	}
}


	/**
	 * Warning: the first execution of this method take ~7.5 million years
	 * @return int|null
	 */
	public function getAnswerToUltimateQuestionOfLifeUniverseAndEverything():?int {
		return Yii::$app->cache->getOrSet(
			'DeepThought::AnswerToUltimateQuestionOfLifeUniverseAndEverything',
			function() {
				return $this->multiple(6, 9);//42
			}
		);
	}

/**
 * @property mixed AnswerToUltimateQuestionOfLifeUniverseAndEverything
 * @property-write mixed $alpha
 * @property-write mixed $beta
 * @property-write mixed $base
 */
class DeepThought extends \yii\base\Model {
	private mixed $_alpha = 6;
	private mixed $_beta = 9;
	private mixed $_base = 13;

	/**
	 * Warning: this method execution take... idk, honestly
	 * @return mixed
	 */
	public function getAnswerToUltimateQuestionOfLifeUniverseAndEverything():mixed {
		return $this->multiple($this->_alpha, $this->_beta, $this->_base);//???
	}

	/**
	 * @param mixed $value
	 * @return void
	 */
	public function setAlpha(mixed $value):void {
		$this->_alpha = $value;
	}

	/**
	 * @param mixed $value
	 * @return void
	 */
	public function setBeta(mixed $value):void {
		$this->_beta = $value;
	}

	/**
	 * @param mixed $value
	 * @return void
	 */
	public function setBase(mixed $value):void {
		$this->_base = $value;
	}

}

(new DeepThought())->AnswerToUltimateQuestionOfLifeUniverseAndEverything;//ok, 42?
(new DeepThought([
	'alpha' => 'cheese',
	'beta' => 'moon',
	'base' => 'infinity'
]))->AnswerToUltimateQuestionOfLifeUniverseAndEverything;//???

/**
 * @property mixed AnswerToUltimateQuestionOfLifeUniverseAndEverything
 * @property-write mixed $alpha
 * @property-write mixed $beta
 * @property-write mixed $base
 */
class DeepThought extends \yii\base\Model {
	use CachedPropertiesTrait;
	
    private mixed $_alpha = 6;
	private mixed $_beta = 9;
	private mixed $_base = 13;

	/**
	 * @inheritDoc
	 */
	public function cachedProperties():array {
		return [
			/* property cached, cache invalidates, when alpha/beta/base values are changed */
			['AnswerToUltimateQuestionOfLifeUniverseAndEverything', ['alpha', 'beta', 'base']]
		];
	}

	/**
	 * Warning: this method execution take ~7.5 million years
	 * @return mixed
	 */
	public function getAnswerToUltimateQuestionOfLifeUniverseAndEverything():mixed {
		return $this->multiple($this->_alpha, $this->_beta, $this->_base);//???
	}

	/**
	 * @param mixed $value
	 * @return void
	 */
	public function setAlpha(mixed $value):void {
		$this->_alpha = $value;
	}

	/**
	 * @param mixed $value
	 * @return void
	 */
	public function setBeta(mixed $value):void {
		$this->_beta = $value;
	}

	/**
	 * @param mixed $value
	 * @return void
	 */
	public function setBase(mixed $value):void {
		$this->_base = $value;
	}

}