Is dit OOP waardig?

Gestart doorDylan2 reacties66 weergaven

3 berichten · pagina 1 van 1

Dylan

Lid · Topicstarter

Berichten
14.025
Reputatie
1
Punten
0,01
Lid sinds
Laatst gezien
Is dit OOP waardig?

Boostrap.php

Codeblok (voorbeeld.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


/* Autoload the classes */

class Bootstrap {

/**
* @param string $className
*/


public static function load($className) {
if(strpos($className, jRouter_) !== FALSE) {
$className = preg_replace(/jRouter_/, , $className);
$className = preg_replace(/_/, /, $className);

if(is_readable(__DIR__ . "/{$className}/{$className}.class.php")) {
require_once(__DIR__ . "/{$className}/{$className}.class.php");
}
else {
throw new Exception("The class {$className} could not be loaded!");
}
}
}


public static function register() {
spl_autoload_register(function($className) {
self::load($className);
});
}
}


Bootstrap::register();

Params.class.php

Codeblok (voorbeeld.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72


class jRouter_Params {

/**
* @var array $errorList
* This variable stores the errors
*/


public $errorList = array();

/**
* @var boolean $isValid
*/


public $isValid = FALSE;

public function __construct($params = array()) {
if(!empty($params)) {
foreach($params as $method => $key) {
switch(strtoupper($method)) {
case
GET:
foreach($key as $param => $options) {

/* Check if the name options is set */
if(isset($options[name])) {
$paramName = $options[name];
}
else {
$paramName = $param;
}


/* Check if the param is set */
if(isset($options[required]) && $options[required] === TRUE) {
if(!isset($_GET[$param])) {
$this->errorList[] = "The param {$paramName} was not founded!";
}
}

}
default:

break;
case
POST:
break;
}
}


if(empty($errorList)) {
$this->isValid = TRUE;
return;
}
}


$this->isValid = FALSE;
}


/**
* @return array $errorList
*/


public function getErrors() {
return $this->errorList;
}


/**
* @param array $params
* @return boolean
*/


public function isValid() {
return $this->isValid;
}
}


DisplayErrors.class.php

Codeblok (voorbeeld.php):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


class jRouter_DisplayErrors {

/**
* @var array $errors
* @return string
*/


public static function asString($errors = array()) {
$str = ;

foreach($errors as $error) {
$str .= $error .
;
}


return $str;
}
}


Enige tips zijn ook welkom!
0
Berichten
4.214
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Singletons zou ik niet gebruiken, ook je autoloader is wat vreemd. De PSR-4 standaard kun je beter gebruiken.
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Absoluut niet OOP waardig.

1) Bootstrap != autoloader. Een bootstrap zit in je index.php bestand en heeft het doel om de autoloader zelf in te laden, niet de autoloader te zijn.

2) wtf moet "jRouter_Params" voorstellen als classnaam? Je hardcode ook error messages, die moeten in een andere classe. Je controleert of GET of POST parameters bestaan. Dit kan je al opslitsen.

- je maakt een ParameterBag classe die als doel heeft om $_POST en $_GET variables te storen. Die hardcode je niet in je classe, je geeft ze mee als parameters.

- je hernoemt "jRouter_Params" (overigens een kutnaam voor een classe, zie PSR standaarden) naar "ParameterBagValidator", want die class heeft als doel om de parameter bag (= dus de POST of GET request) te valideren.

3) jRouter_DisplayErrors is een nutteloze class, een router mag geen verantwoordelijkheid hebben om errors te displayen, een goede router zou een exception gooien die je kunt opvangen en vervolgens de fout handlen via je eigen shit.

4) kijk naar namespaces. ipv jRouter_ParameterBag zou je dan hebben, JRouterParameterBag.

5) als je dan namespaces gebruikt kun je ook gebruik maken van de PSR autoloader.

6) maak een request class. die stelt een inkomende request voor. die heeft de variables: $post en $get die allebei ParameterBag instances zijn.

Voorbeeldje:

[COLOR= #0000BB]<?php

$get[/COLOR] [COLOR= #007700]= new[/COLOR] ParameterBag[COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$post[/COLOR] [COLOR= #007700]= new[/COLOR] ParameterBag[COLOR= #007700]([/COLOR][COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700]);[/COLOR]

[COLOR= #0000BB]$request[/COLOR] [COLOR= #007700]= new[/COLOR] Request[COLOR= #007700]([/COLOR][COLOR= #0000BB]$get[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$post[/COLOR][COLOR= #007700]);[/COLOR] [COLOR= #FF8000]// je kan ook eventueel hiervan een static helper maken zoals Request::createFromGetAndPost of iets dergelijks.[/COLOR]

[COLOR= #0000BB]$validator[/COLOR] [COLOR= #007700]= new[/COLOR] ParameterBagValidator[COLOR= #007700]([/COLOR][COLOR= #0000BB]$post[/COLOR][COLOR= #007700], [[/COLOR][COLOR= #DD0000]username[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]password[/COLOR][COLOR= #007700]]);[/COLOR]

[URL="http://www.php.net/if"]if[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$validator[/COLOR][COLOR= #007700]->[/COLOR]isValid[COLOR= #007700]()) {[/COLOR]
  [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]hoi![/COLOR][COLOR= #007700];
}[/COLOR] [URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]
  [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]fout![/COLOR][COLOR= #007700];[/COLOR] [COLOR= #FF8000]// foutmelding horen dus NIET in de classe zelf terecht zoals in jouw code.[/COLOR]
[COLOR= #007700]}[/COLOR]


toevoeging op 19 augustus 2014, 131403:
Hopelijk heb je er wat aan. Als je vragen hebt; stel ze.

toevoeging op 19 augustus 2014, 131944:
Kijk ook hoe Symfony het doet: https://github.com/symfony/HttpFoundation
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren