Milan schreef:Aantal tips:Eerdere citaten tonen
Stef schreef:Ik heb op dit moment een class die er ongeveer zo uitziet:[COLOR= #0000BB]<?php[/COLOR] [URL="http://www.php.net/class"]class[/URL] Account [COLOR= #007700]{[/COLOR] [URL="http://www.php.net/public"]public[/URL] [COLOR= #0000BB]$eigenschap1[/COLOR][COLOR= #007700];[/COLOR] [URL="http://www.php.net/public"]public[/URL] [COLOR= #0000BB]$eigenschap2[/COLOR][COLOR= #007700];[/COLOR] [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] __construct[COLOR= #007700]([/COLOR][COLOR= #0000BB]$id[/COLOR][COLOR= #007700]) {[/COLOR] [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/is_numeric"]is_numeric[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$id[/COLOR][COLOR= #007700])) {[/COLOR] [COLOR= #0000BB]$db[/COLOR] [COLOR= #007700]= new[/COLOR] Database[COLOR= #007700]([/COLOR][COLOR= #DD0000]"Databasenaam"[/COLOR][COLOR= #007700]);[/COLOR] [COLOR= #0000BB]$query[/COLOR] [COLOR= #007700]= ;[/COLOR][COLOR= #FF8000]//Hier wordt een query uitgevoerd enzo[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]eigenschap1 [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$query[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]][[/COLOR][COLOR= #DD0000]eigenschap1[/COLOR][COLOR= #007700]];[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]-[/COLOR]eigenschap2 [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$query[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]][[/COLOR][COLOR= #DD0000]eigenschap2[/COLOR][COLOR= #007700]];[/COLOR] [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR] [COLOR= #007700]===[/COLOR] false[COLOR= #007700]) {[/COLOR] [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Error: query returned false."[/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]"Wrong use of function. Use an integer as parameter."[/COLOR][COLOR= #007700]; } }[/COLOR]
Wanneer ik dus een lijstje met accounts wil maken, vraag ik eerst alle account IDs op en daarna initialiseer ik voor ieder ID een account class.
Maar gebruik ik dan niet overdreven veel querys? Is er een nettere manier om dit te doen?
- Gebruik excepties i.p.v. echo.
- Dependicy injection
- Ik zou een aparte class aanmaken voor databaseverkeer, bijvoorbeeld AccountDAO (DataAccessObject), hierin kan je dan bijvoorbeeld een functie maken met 1 query alle accounts ophaalt en een array met Account object terugstuurt.
Ik maak op dit moment gebruik van deze Database class:
[COLOR= #0000BB]<?php[/COLOR]
[COLOR= #FF8000]/**
* Created by PhpStorm.
* User: Stef van der Wel
* Date: 23-5-2016
* Time: 11:15
*/[/COLOR]
[URL="http://www.php.net/class"]class[/URL] Database
[COLOR= #007700]{[/COLOR]
[URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$host[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]"xxx.xxx.x.xx"[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$port[/COLOR] [COLOR= #007700]=[/COLOR] xxxx[COLOR= #007700];[/COLOR]
[URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$username[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]"xx"[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$password[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]"xx"[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #0000BB]$database[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #0000BB]$connection[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] __construct[COLOR= #007700]([/COLOR][COLOR= #0000BB]$database[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]"StandaardDatabase"[/COLOR][COLOR= #007700])
{[/COLOR]
[COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]database [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$database[/COLOR][COLOR= #007700];[/COLOR]
try [COLOR= #007700]{[/COLOR]
[COLOR= #FF8000]// $this->connection = new PDO("dblib:host=$this->host;dbname=$this->database;charset=utf8mb4", $this->username, $this->password);
// $this->connection = new PDO("dblib:dbname=$this->database;host=$this->host", $this->username, $this->password);[/COLOR]
[COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]connection [COLOR= #007700]= new[/COLOR] PDO [COLOR= #007700]([/COLOR][COLOR= #DD0000]"dblib:host=$this->host:$this->port;dbname=$this->database;charset=UTF-8"[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]"$this->username"[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]"$this->password"[/COLOR][COLOR= #007700]);
}[/COLOR] catch[COLOR= #007700]([/COLOR]PDOException [COLOR= #0000BB]$e[/COLOR][COLOR= #007700]) {[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Connection to the database failed"[/COLOR][COLOR= #007700];
}
}[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] insert[COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$par[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]()) {[/COLOR]
[COLOR= #0000BB]$query[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]connection[COLOR= #007700]->[/COLOR]prepare[COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$execute[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$query[/COLOR][COLOR= #007700]->[/COLOR]execute[COLOR= #007700]([/COLOR][COLOR= #0000BB]$par[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$execute[/COLOR][COLOR= #007700];
}[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] select[COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$par[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]()) {[/COLOR]
[COLOR= #0000BB]$query[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]connection[COLOR= #007700]->[/COLOR]prepare[COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$execute[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$query[/COLOR][COLOR= #007700]->[/COLOR]execute[COLOR= #007700]([/COLOR][COLOR= #0000BB]$par[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$execute[/COLOR] [COLOR= #007700]==[/COLOR] true[COLOR= #007700]) {[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$query[/COLOR][COLOR= #007700]->[/COLOR]fetchAll[COLOR= #007700]();
}[/COLOR] [URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]
[COLOR= #FF8000]// return $query->fetchAll(); //false[/COLOR]
[URL="http://www.php.net/return"]return[/URL] false[COLOR= #007700];
}
}[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] delete[COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$par[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]()) {[/COLOR]
[COLOR= #0000BB]$query[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]connection[COLOR= #007700]->[/COLOR]prepare[COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$execute[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$query[/COLOR][COLOR= #007700]->[/COLOR]execute[COLOR= #007700]([/COLOR][COLOR= #0000BB]$par[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$execute[/COLOR][COLOR= #007700];
}[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] update[COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$par[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]()) {[/COLOR]
[COLOR= #0000BB]$query[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]connection[COLOR= #007700]->[/COLOR]prepare[COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$execute[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$query[/COLOR][COLOR= #007700]->[/COLOR]execute[COLOR= #007700]([/COLOR][COLOR= #0000BB]$par[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$execute[/COLOR][COLOR= #007700];
}
}[/COLOR] Ik heb even gegoogled naar Dependency Injection. Wat ik ervan begrijp is dat je gebruik maakt van een andere class waar je data van krijgt, klopt dat?
Dit doe ik dan in principe met een Database class, toch.
Wat ik zou kunnen doen is een functie maken getUserList() en die dan een array laten returnen? Moet die functie dan in de Account class?
Maar zoals je op regel 30 doet gaat m niet worden. De echte account class heeft 19 eigenschappen die ik allemaal uit de database moet ophalen. Die kan ik lastig bij het initialiseren meegeven

toevoeging op donderdag 1 december 2016, 11:45:21:
Maar in ieder geval bedankt voor je tips!

Ik heb net
dan dat je er even snel via Configuratiescherm of Rechtsklik->Beeldscherminstellingen naar toe klikt.


