Klopt, de :: operator is voor static aanroepen. Als je $bla = new Class1(); doet kun je vervolgens $bla->foo(); gebruiken. $this werkt alleen voor methodes en properties binnen de huidige klasse, tenzij je uitbreid op een bepaalde klasse: class2 extends class1.quote:Op maandag 9 maart 2009 16:50 schreef Chandler het volgende:
ik snap nu wat je bedoelt, echter kun je in het gebruik van class1::foo() geen $this-> gebruiken (heb ik ergens gelezen).
Ow en dat je niet kunt zien dat ik al jaren script is helaas een van mijn zwakke punten. Zoals de meeste posters hier wel weten leer ik door te doen, lezen is helaas het vak waarop ik nooit zal slagen ongeacht ik uren door de teksten heen loop..
Maar goed, ik doe mijn best om alles op te vatten en te begrijpen en tja meer kan ik niet hé..
Ow... en dan nog mijn ervaring; dat heeft zich al geuit in vele goedlopende sites
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | class tinyMySQL { // var $host; var $user; var $password; var $database; var $connectionLink; var $databaseLink; function __construct($host, $username, $password) { if (!$this->connectionLink = @mysql_connect($host, $username, $password)) { throw new Exception('Kan niet connecten met database server.'); } } function select_db($database) { if ($this->connectionLink) { if (!$this->databaseLink = mysql_select_db($database, $this->connectionLink)) { throw new Exception('Kan database koppeling niet aanmaken'); } } else { throw new Exception('Geen database connectie beschikbaar om database te laden'); } } function query($sql) { if ($this->connectionLink) { if (!$result = mysql_query($sql, $this->connectionLink)) { throw new Exception('Error in query: ' . $sql); } } else { throw new Exception('Geen database connectie beschikaar'); } } function __destruct() { if ($this->connectionLink) { mysql_close($this->connectionLink); unset($this->connectionLink); } else { throw new Exception('Geen database connectie beschikaar'); } } } class test { public function testIt() { $x = tinyMySQL::query("SELECT * FROM users"); } } $t = new test(); $con = new TinyMySQL("localhost", "root", ""); if ($con) { $con->select_db("nl_visfreaks"); $t->testIt(); } else { echo 'kan geen connectie maken'; } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class test { public function testIt() { $con = new TinyMySQL("localhost", "root", ""); if($con) { $con->select_db("nl_visfreaks"); return $con->query("SELECT * FROM users"); } } } $t = new test(); $resultaat = $t->testIt(); // Doe hier iets met $resultaat ?> |
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 | { function __construct($server, $user, $password) { //... } } class Forum { var $db; function __construct($db) { $this->db = $db; } public function readTopics() { $topics = $this->db->query(...); return $topics; } } $db = new Database("server", "user", "password"); $forum = new Forum($db); $topics = $forum->readTopics(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | { public static function create() { return new Database("server", "user", "password"); } } class Forum { public function readTopics() { $db = Database::create(); $topics = $db->query(...); return $topics; } } $forum = new Forum(); $topics = $forum->readTopics(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Database { private static $instance = null; public static function getInstance() { if(empty(Database::$instance)) { Database::$instance = new Database(); } return Database::$instance; } private function __construct() { // Private constructor, zo kun je niet per ongeluk zelf "new Database()" aanroepen. } } ?> |
1 2 3 4 5 6 7 | class Forum { public function readTopic() { $db = Database::getInstance(); } } ?> |
Volgens mij trek je daar wel heel veel databaseverbindingen mee open. En da's verspilling van resources. Het maakt het ook lastiger om bijvoorbeeld een lijst van alle queries op te vragen.quote:Op maandag 9 maart 2009 20:50 schreef Xcalibur het volgende:
Ik gebruik optie 1 altijd, daar heb ik wel goede ervaringen mee
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | class tinyMySQL { // private $hostname = null; private $username = null; private $password = null; private $database = null; private $db_con = null; // check public $queries = array(); function __construct($hostname = '', $username = '', $password = '', $database = '') { if (!empty($hostname) && !empty($username)) { $this->connect($hostname, $username, $password); if ($this->db_con && !empty($database)) { if (!$this->select_db($database)) { trigger_error('Kan database niet laden, probeer het nogmaals', E_USER_NOTICE); } } } } public function connect($hostname, $username, $password) { if (!empty($hostname) && !empty($username)) { $this->db_con = mysql_connect($hostname, $username, $password); if (!$this->db_con) { return false; } $this->hostname = $hostname; $this->username = $username; $this->password = $password; return true; } else { trigger_error('Kan geen connectie maken met de database server met incomplete gegevens', E_USER_NOTICE); } } function select_db($database) { if ($this->db_con) { $try_select = @mysql_select_db($database, $this->db_con); if ($try_select) { $this->database = $database; return true; } else { trigger_error('Kan database koppeling niet aanmaken', E_USER_NOTICE); } } else { trigger_error('Geen database connectie beschikbaar om database te laden', E_USER_NOTICE); } return false; } public function query($sql, $unbuffered = false) { if ($this->db_con) { // lees query bench totaal items $x = count($this->queries)+1; // queries bench data $this->queries[$x] = array("query" => $sql, "start" => time(), "end" => 0); // wat voor query is het en voer het uit if ($unbuffered == true) { $query_status = @mysql_unbuffered_query($sql, $this->db_con); } else { $query_status = @mysql_query($sql, $this->db_con); } // eind tijd. $this->queries[$x]['end'] = time(); $this->queries[$x]['time'] = round($this->queries[$x]['end'] - $this->queries[$x]['start'], 4); // query in de fout? sla fout in bench op. if (!$query_status) { // error gevonden dus error in query array zetten $this->queries[$x]['error'] = mysql_error(); $this->queries[$x]['errno'] = mysql_errno($this->db_con); $this->queries[$x]['minfo'] = mysql_info($this->db_con); } return $query_status; } else { trigger_error('Geen database connectie beschikaar', E_USER_NOTICE); return false; } } public function escape($string) { if ($this->db_con) { if (function_exists('mysq_real_escape_string')) { return @mysql_real_escape_string($string); } else { return @mysql_escape_string($string); } } else { trigger_error('Geen database connectie beschikaar', E_USER_NOTICE); return false; } } public function affected_rows() { if ($this->db_con) { $status = @mysql_affected_rows($this->db_con); if ($status != false) { $this->queries[count($this->queries)]['affected_rows'] = $status; } return $status; } else { trigger_error('Geen database connectie beschikaar', E_USER_NOTICE); return false; } } public function fetch_array($result) { return @mysql_fetch_array($result); } public function fetch_assoc($result) { return @mysql_fetch_assoc($result); } public function fetch_object($result) { return @mysql_fetch_object($result); } public function result($result, $start = 0) { $items = @mysql_result($result, $start); if ($items != false) { $this->queries[count($this->queries)]['result'] = $items; } return $items; } public function last_id() { if ($this->con) { return @mysql_insert_id($this->db_con); } else { trigger_error('Geen database connectie beschikaar', E_USER_NOTICE); return false; } } public function num_rows($result) { if ($this->db_con) { $items = @mysql_num_rows($result); if ($items != false) { $this->queries[count($this->queries)]['items'] = $items; } return $items; } else { trigger_error('Geen database connectie beschikaar', E_USER_NOTICE); return false; } } public function close() { if ($this->db_con) { $try_close = @mysql_close($this->db_con); if ($try_close) { $this->db_hostname = null; $this->db_username = null; $this->db_password = null; $this->db_database = null; $this->db_con = null; return true; } else { return false; } } else { trigger_error('Kan geen database koppeling sluiten, geen koppeling gevonden', E_USER_NOTICE); } } function __destruct() { $this->close(); } } ?> |
Maar nog steeds snap ik het voordeel van (!$var) niet ipv ($var == false)quote:Op dinsdag 10 maart 2009 09:11 schreef Catch22- het volgende:
Het is wat korter/netter.
Als je het serieus wil aanpakken kan je beter loggen. Users moeten in principe geen errors zien.
en over commentaar; 'If it was hard to write, it should be hard to understand'
Dan kan je net zo goed (($var == false) == true) schrijven.quote:Op dinsdag 10 maart 2009 09:18 schreef Chandler het volgende:
Maar nog steeds snap ik het voordeel van (!$var) niet ipv ($var == false)
Uiteraardquote:Op dinsdag 10 maart 2009 13:06 schreef Catch22- het volgende:
en dat heeft het bij een boolean niet. of hij moet TRUE zijn, of true, of TruE![]()
Forum Opties | |
---|---|
Forumhop: | |
Hop naar: |