statyczny cache vs zwykły w php

<?php

class test {
	private static $cache = null;
	
	public function getData() {
		if(null == self::$cache) {
			self::$cache = $this->getFromDb();
		}
		
		return self::$cache;
	}
	
	public function getFromDb() {
		echo 'pobieranie danych'."\n";
		return 1234;
	}
}


class test2 {
	private  $cache = null;
	
	public function setCache(){
		self::$cache = 123;
	}
	
	public function getData() {
		if(null == $this->cache) {
			$this->cache = $this->getFromDb();
		}
		
		return $this->cache;
	}
	
	public function getFromDb() {
		echo 'pobieranie danych2222'."\n";
		return 12345678;
	}
}

$test = new test();
echo $test->getData();
$test2 = new test();
echo $test2->getData();
echo "\n\n\n";
//==============
$test3 = new test2();
echo $test3->getData() ."\n";
$test3 = new test2();
echo $test3->getData();
Komentarze wyłączone