Mijn eerste OOP
Ik ben vandaag begonnen met OOP, na een uurtje of 2 gewerkt te hebben aan een scriptje zou ik graag jullie commentaar/tips willen op mijn script, alvast bedankt.
index.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
include ('cache.class.php');
$cache = new Cache;
$cache->setCacheTime(10);
if ($cache->existCheck() && $cache->validCheck()) {
include($cache->getCachefile());
exit;
}
ob_start();
?>
<html>
<head>
Head.
</head>
<body>
</body>
</html>
<?php
$cache->writeCache(ob_get_contents());
ob_end_flush();
?>
include ('cache.class.php');
$cache = new Cache;
$cache->setCacheTime(10);
if ($cache->existCheck() && $cache->validCheck()) {
include($cache->getCachefile());
exit;
}
ob_start();
?>
<html>
<head>
Head.
</head>
<body>
</body>
</html>
<?php
$cache->writeCache(ob_get_contents());
ob_end_flush();
?>
cache.class.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
class Cache {
private $_cachefile;
private $_cacheTime;
//Cache will be made of the current file.
public function __construct() {
$this->_cachefile = 'cache' . $_SERVER['PHP_SELF'];
}
//Sets the lifetime of the cached file.
public function setCacheTime($time) {
$this->_cacheTime = $time;
}
//Returns the name of the cachefile.
public function getCachefile() {
return $this->_cachefile;
}
//Returns the name of the lifetime of the cached file.
public function getCacheTime() {
return $this->_cacheTime;
}
//Returns true cache is valid.
public function validCheck() {
if (time() - $this->_cacheTime < filemtime($this->_cachefile)) {
return true;
}
else {
return false;
}
}
//Returns true if file exists.
public function existCheck() {
if (file_exists($this->_cachefile)) {
return true;
}
else {
return false;
}
}
//Writes the cache to the file.
public function writeCache($text) {
if (empty($this->_cacheTime)) {
echo '<br/><b>Error</b>: Function: "' . __FUNCTION__ . '" requires CacheTime, please set with function "setCacheTime"';
exit;
}
$fp = fopen($this->getCachefile(), 'w');
fwrite($fp, '<!--Cached ' . date("d-m-Y H:i:s",time()+3600) . '--> ' . $text);
fclose($fp);
}
}
?>
class Cache {
private $_cachefile;
private $_cacheTime;
//Cache will be made of the current file.
public function __construct() {
$this->_cachefile = 'cache' . $_SERVER['PHP_SELF'];
}
//Sets the lifetime of the cached file.
public function setCacheTime($time) {
$this->_cacheTime = $time;
}
//Returns the name of the cachefile.
public function getCachefile() {
return $this->_cachefile;
}
//Returns the name of the lifetime of the cached file.
public function getCacheTime() {
return $this->_cacheTime;
}
//Returns true cache is valid.
public function validCheck() {
if (time() - $this->_cacheTime < filemtime($this->_cachefile)) {
return true;
}
else {
return false;
}
}
//Returns true if file exists.
public function existCheck() {
if (file_exists($this->_cachefile)) {
return true;
}
else {
return false;
}
}
//Writes the cache to the file.
public function writeCache($text) {
if (empty($this->_cacheTime)) {
echo '<br/><b>Error</b>: Function: "' . __FUNCTION__ . '" requires CacheTime, please set with function "setCacheTime"';
exit;
}
$fp = fopen($this->getCachefile(), 'w');
fwrite($fp, '<!--Cached ' . date("d-m-Y H:i:s",time()+3600) . '--> ' . $text);
fclose($fp);
}
}
?>
- Verder zie ik dit als een verzameling van functies in een class. Dit is geen OOP. OOP is vooral het denken in objecten: Elk object is een class, elk eigenschap is een propertie en elk verandering is een method.
- Je class is al cache, dus de properties moeten niet _cacheiets heten maar _iets. Dit zelfde geld ook voor de functies. Je hebt het object cache en als je de file wilt krijgen gebruik je cache::getFile().
Wouter J op 01/02/2011 17:59:19:
- Doe nooit een echo in een method. Je moet zo'n waarde altijd returnen. Mooier zou zijn als je gebruikt maakt van een eigen foutafhandeling met bijv. een method getError() en nog mooier is gebruik maken van exceptions en daarmee de error opvangen.
- Verder zie ik dit als een verzameling van functies in een class. Dit is geen OOP. OOP is vooral het denken in objecten: Elk object is een class, elk eigenschap is een propertie en elk verandering is een method.
- Je class is al cache, dus de properties moeten niet _cacheiets heten maar _iets. Dit zelfde geld ook voor de functies. Je hebt het object cache en als je de file wilt krijgen gebruik je cache::getFile().
- Verder zie ik dit als een verzameling van functies in een class. Dit is geen OOP. OOP is vooral het denken in objecten: Elk object is een class, elk eigenschap is een propertie en elk verandering is een method.
- Je class is al cache, dus de properties moeten niet _cacheiets heten maar _iets. Dit zelfde geld ook voor de functies. Je hebt het object cache en als je de file wilt krijgen gebruik je cache::getFile().
Bedankt voor je reactie, heb ik veel aan.
Nog andere mensen die er wat in zien ? :)
Meestal is de cache klasse een cache-manager, geen cache-item. Het is dus meer
$cache->set($name, $value[, $lifetime]) en $cache->get($name). Jouw keuze maakt meer abstractie (bijvoorbeeld het aanbieden van meerdere opslagmechanismen) ingewikkelder.
Pim - op 01/02/2011 22:44:13:
Je kan de naam van de cache beter injecteren injecteren in je constructor: Cache::__construct($fileName)
Meestal is de cache klasse een cache-manager, geen cache-item. Het is dus meer
$cache->set($name, $value[, $lifetime]) en $cache->get($name). Jouw keuze maakt meer abstractie (bijvoorbeeld het aanbieden van meerdere opslagmechanismen) ingewikkelder.
Meestal is de cache klasse een cache-manager, geen cache-item. Het is dus meer
$cache->set($name, $value[, $lifetime]) en $cache->get($name). Jouw keuze maakt meer abstractie (bijvoorbeeld het aanbieden van meerdere opslagmechanismen) ingewikkelder.
Oke klinkt goed, het kwartje valt, maar ik snap het verschil tussen "::" en "->" nog niet helemaal, wanner gebruik je nu precies "::"?