Epower Skrevet 17. desember 2008 Del Skrevet 17. desember 2008 Tror ikke du trenger $this -> connection -> quote() <?php class Guestbook { private $numEntriesPerPage = 10; private $connection; private $entries; private $memcacheConnection; public function __construct(PDO $connection) { $this->connection = $connection; } public function addEntry(GuestbookEntry $entry) { $this->entries[] = $entry; } public function fetchEntries(array $options = array()) { if ( $this->mcConnected ) {} $sql = "SELECT * FROM guestbook;"; $stmt = $this->connection->prepare($sql); $stmt->execute(); $this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry'); } public function __destruct () { for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) { if ( empty ( $this -> entries[$i] -> id ) ) { $sql = sprintf ( "INSERT INTO entries (author, email, website, message, ip) VALUES('%s', '%s', '%s', '%s', INET_ATON('%s'))", $this -> entries[$i] -> author, $this -> entries[$i] -> email, $this -> entries[$i] -> website, $this -> entries[$i] -> message, $_SERVER['REMOTE_ADDR']); } } } } class GuestbookEntry { public $id; public $author; public $mail; public $message; public $date; public $ip; public function __set($key, $value) { if (isset($this->$key)) { $this->$key = $value; } } } ?> Lenke til kommentar
Alex Moran Skrevet 17. desember 2008 Del Skrevet 17. desember 2008 (endret) Bruk heller prepared statements <?php class Guestbook { private $numEntriesPerPage = 10; private $connection; private $entries; private $memcacheConnection; public function __construct(PDO $connection) { $this->connection = $connection; } public function addEntry(GuestbookEntry $entry) { $this->entries[] = $entry; } public function fetchEntries(array $options = array()) { if ( $this->mcConnected ) {} $sql = "SELECT * FROM guestbook;"; $stmt = $this->connection->prepare($sql); $stmt->execute(); $this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry'); } public function __destruct () { for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) { if ( empty ( $this -> entries[$i] -> id ) ) { $sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))"; $stmt = $this->connection->prepare($sql); $stmt->execute(array( $this->entries[$i]->author, $this->entries[$i]->email, $this->entries[$i]->website, $this->entries[$i]->message, $_SERVER['REMOTE_ADDR'] )); } } } } class GuestbookEntry { public $id; public $author; public $mail; public $message; public $date; public $ip; public function __set($key, $value) { if (isset($this->$key)) { $this->$key = $value; } } } ?> Endret 17. desember 2008 av Josh Homme Lenke til kommentar
LarsAndre Skrevet 17. desember 2008 Del Skrevet 17. desember 2008 (endret) <?php (...) class GuestbookEntry { public $id; public $author; public $mail; public $message; public $date; public $ip; public function __set($key, $value) { if (isset($this->$key)) { $this->$key = $value; } } } ?> Hva er meningen med __set? Endret 17. desember 2008 av LarsAndre Lenke til kommentar
Epower Skrevet 17. desember 2008 Del Skrevet 17. desember 2008 __set La til $website; <?php class Guestbook { private $numEntriesPerPage = 10; private $connection; private $entries; private $memcacheConnection; public function __construct(PDO $connection) { $this->connection = $connection; } public function addEntry(GuestbookEntry $entry) { $this->entries[] = $entry; } public function fetchEntries(array $options = array()) { if ( $this->mcConnected ) {} $sql = "SELECT * FROM guestbook;"; $stmt = $this->connection->prepare($sql); $stmt->execute(); $this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry'); } public function __destruct () { for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) { if ( empty ( $this -> entries[$i] -> id ) ) { $sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))"; $stmt = $this->connection->prepare($sql); $stmt->execute(array( $this->entries[$i]->author, $this->entries[$i]->email, $this->entries[$i]->website, $this->entries[$i]->message, $_SERVER['REMOTE_ADDR'] )); } } } } class GuestbookEntry { public $id; public $author; public $mail; public $website; public $message; public $date; public $ip; public function __set($key, $value) { if (isset($this->$key)) { $this->$key = $value; } } } ?> Lenke til kommentar
LarsAndre Skrevet 17. desember 2008 Del Skrevet 17. desember 2008 (endret) __set Jeg er klar over det, men hvis du ser på hva denne funksjonen gjør i dette tilfellet er den ganske unyttig. Alle variablene i den klassen er public så det er vell dumt å ta en omvei? Endret 17. desember 2008 av LarsAndre Lenke til kommentar
JohndoeMAKT Skrevet 18. desember 2008 Del Skrevet 18. desember 2008 La til GET fra MC. <?php class Guestbook { private $numEntriesPerPage = 10; private $connection; private $entries; private $memcacheConnection; public function __construct(PDO $connection) { $this->connection = $connection; } public function addEntry(GuestbookEntry $entry) { $this->entries[] = $entry; } public function fetchEntries(array $options = array()) { if ( $this->mcConnected ) { $entries = $this->memcacheConnection->get( 'gb-entries' ); } $sql = "SELECT * FROM guestbook;"; $stmt = $this->connection->prepare($sql); $stmt->execute(); $this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry'); } public function __destruct () { for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) { if ( empty ( $this -> entries[$i] -> id ) ) { $sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))"; $stmt = $this->connection->prepare($sql); $stmt->execute(array( $this->entries[$i]->author, $this->entries[$i]->email, $this->entries[$i]->website, $this->entries[$i]->message, $_SERVER['REMOTE_ADDR'] )); } } } } class GuestbookEntry { public $id; public $author; public $mail; public $website; public $message; public $date; public $ip; public function __set($key, $value) { if (isset($this->$key)) { $this->$key = $value; } } } ?> Lenke til kommentar
Epower Skrevet 18. desember 2008 Del Skrevet 18. desember 2008 Er det sånn det skal være? private $memcacheConnection = new Memcache; <?php class Guestbook { private $numEntriesPerPage = 10; private $connection; private $entries; private $memcacheConnection = new Memcache; public function __construct(PDO $connection) { $this->connection = $connection; } public function addEntry(GuestbookEntry $entry) { $this->entries[] = $entry; } public function fetchEntries(array $options = array()) { if ( $this->mcConnected ) { $entries = $this->memcacheConnection->get( 'gb-entries' ); } $sql = "SELECT * FROM guestbook;"; $stmt = $this->connection->prepare($sql); $stmt->execute(); $this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry'); } public function __destruct () { for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) { if ( empty ( $this -> entries[$i] -> id ) ) { $sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))"; $stmt = $this->connection->prepare($sql); $stmt->execute(array( $this->entries[$i]->author, $this->entries[$i]->email, $this->entries[$i]->website, $this->entries[$i]->message, $_SERVER['REMOTE_ADDR'] )); } } } } class GuestbookEntry { public $id; public $author; public $mail; public $website; public $message; public $date; public $ip; public function __set($key, $value) { if (isset($this->$key)) { $this->$key = $value; } } } ?> Lenke til kommentar
JohndoeMAKT Skrevet 18. desember 2008 Del Skrevet 18. desember 2008 Er det sånn det skal være? private $memcacheConnection = new Memcache; Det er mulig ja. Muligens den bør settes i __construct på samme måte som $connection, og at databaseconnection blir opprettet on demand, men det kan endres siden. <?php class Guestbook { private $numEntriesPerPage = 10; private $connection; private $entries; private $memcacheConnection = new Memcache; public function __construct(PDO $connection) { $this->connection = $connection; } public function addEntry(GuestbookEntry $entry) { $this->entries[] = $entry; } public function fetchEntries(array $options = array()) { if ( $this->mcConnected ) { $entries = $this->memcacheConnection->get( 'gb-entries' ); if ( $entries !== false ) {} } $sql = "SELECT * FROM guestbook;"; $stmt = $this->connection->prepare($sql); $stmt->execute(); $this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry'); } public function __destruct () { for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) { if ( empty ( $this -> entries[$i] -> id ) ) { $sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))"; $stmt = $this->connection->prepare($sql); $stmt->execute(array( $this->entries[$i]->author, $this->entries[$i]->email, $this->entries[$i]->website, $this->entries[$i]->message, $_SERVER['REMOTE_ADDR'] )); } } } } class GuestbookEntry { public $id; public $author; public $mail; public $website; public $message; public $date; public $ip; public function __set($key, $value) { if (isset($this->$key)) { $this->$key = $value; } } } ?> Lenke til kommentar
Epower Skrevet 20. desember 2008 Del Skrevet 20. desember 2008 (endret) La til $mcConnected; <?php class Guestbook { private $numEntriesPerPage = 10; private $connection; private $entries; private $memcacheConnection = new Memcache; private $mcConnected; public function __construct(PDO $connection) { $this->connection = $connection; } public function addEntry(GuestbookEntry $entry) { $this->entries[] = $entry; } public function fetchEntries(array $options = array()) { if ( $this->mcConnected ) { $entries = $this->memcacheConnection->get( 'gb-entries' ); if ( $entries !== false ) {} } $sql = "SELECT * FROM guestbook;"; $stmt = $this->connection->prepare($sql); $stmt->execute(); $this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry'); } public function __destruct () { for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) { if ( empty ( $this -> entries[$i] -> id ) ) { $sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))"; $stmt = $this->connection->prepare($sql); $stmt->execute(array( $this->entries[$i]->author, $this->entries[$i]->email, $this->entries[$i]->website, $this->entries[$i]->message, $_SERVER['REMOTE_ADDR'] )); } } } } class GuestbookEntry { public $id; public $author; public $mail; public $website; public $message; public $date; public $ip; public function __set($key, $value) { if (isset($this->$key)) { $this->$key = $value; } } } ?> Endret 20. desember 2008 av Eirikkkkkk Lenke til kommentar
JohndoeMAKT Skrevet 20. desember 2008 Del Skrevet 20. desember 2008 La til returnering av objekter fra MC. Egentlig vil jeg bruke json_decode med stdObject og duck typing, og egentlig-egentlig vil jeg ikke bruke objekter, men den krangelen kan vi ta siden. <?php class Guestbook { private $numEntriesPerPage = 10; private $connection; private $entries; private $memcacheConnection = new Memcache; private $mcConnected; public function __construct(PDO $connection) { $this->connection = $connection; } public function addEntry(GuestbookEntry $entry) { $this->entries[] = $entry; } public function fetchEntries(array $options = array()) { if ( $this->mcConnected ) { $entries = $this->memcacheConnection->get( 'gb-entries' ); if ( $entries !== false ) { return unserialize( $entries ); } } $sql = "SELECT * FROM guestbook;"; $stmt = $this->connection->prepare($sql); $stmt->execute(); $this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry'); } public function __destruct () { for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) { if ( empty ( $this -> entries[$i] -> id ) ) { $sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))"; $stmt = $this->connection->prepare($sql); $stmt->execute(array( $this->entries[$i]->author, $this->entries[$i]->email, $this->entries[$i]->website, $this->entries[$i]->message, $_SERVER['REMOTE_ADDR'] )); } } } } class GuestbookEntry { public $id; public $author; public $mail; public $website; public $message; public $date; public $ip; public function __set($key, $value) { if (isset($this->$key)) { $this->$key = $value; } } } ?> Lenke til kommentar
JohndoeMAKT Skrevet 20. desember 2008 Del Skrevet 20. desember 2008 (endret) Serialisering i PHP med serialize() og unserialize() er ofte tregt, så jeg pleier å bruke json_encode og json_decode (native fra PHP 5.2, og tilgjengelig som en C-kompilert PECL-modul for eldre versjoner) for serialisering. Problemet er bare at JSON bare forteller at noe er et objekt, ikke hvilken klasse objektet er, så du må enten manuelt caste det deserialiserte objektet tilbake til orginal klasse eller bare bruke det som stdClass og duck type når du bruker det. stdObject er PHPs basisklasse eller noe slikt. <?php header( 'Content-type: text/plain' ); class elg {} $fisk = new elg(); $fisk->ulv = 'geit'; echo 'Original class name: ', get_class( $fisk ), "\n\n"; $jsonFisk = json_encode( $fisk ); echo 'JSON serialized: ', $jsonFisk, "\n"; $jsonFisk = json_decode( $jsonFisk ); echo 'JSON unserialized class name: ', get_class( $jsonFisk ), "\n\n"; $serFisk = serialize( $fisk ); echo 'serialize() serialized: ', $serFisk, "\n"; $serFisk = unserialize( $serFisk ); echo 'unserialize() unserialized class name: ', get_class( $serFisk ), "\n"; ?> Output : Original class name: elg JSON serialized: {"ulv":"geit"} JSON unserialized class name: stdClass serialize() serialized: O:3:"elg":1:{s:3:"ulv";s:4:"geit";} unserialize() unserialized class name: elg Les mer om JSON her: http://en.wikipedia.org/wiki/JSON Endret 20. desember 2008 av JohndoeMAKT Lenke til kommentar
Epower Skrevet 20. desember 2008 Del Skrevet 20. desember 2008 Tror jeg skjønte litt mer nå Men hva er ulempene med å bruke stdClass da? Lenke til kommentar
JohndoeMAKT Skrevet 21. desember 2008 Del Skrevet 21. desember 2008 Ulempen viser seg dersom du har et array med objekter av typen elg, hund, katt og rev og json_encode/json_dekoder arrayet. Bruker du serialize() som bevarer klassetypen kan du bruke get_class() for å skille typene fra hverandre, men det kan du ikke dersom alle er av type stdClass, så da må duck typing eller en jukseattributt med klassenavn i hvert objekt til. Lenke til kommentar
Epower Skrevet 21. desember 2008 Del Skrevet 21. desember 2008 Så da bruker vi serialize, da? Lenke til kommentar
Epower Skrevet 21. desember 2008 Del Skrevet 21. desember 2008 Ok <?php class Guestbook { private $numEntriesPerPage = 10; private $connection; private $entries; private $memcacheConnection; private $mcConnected; public function __construct(PDO $connection, $memcacheConnection) { $this->connection = $connection; $this->memcacheConnection = $memcacheConnection; } public function addEntry(GuestbookEntry $entry) { $this->entries[] = $entry; } public function fetchEntries(array $options = array()) { if ( $this->mcConnected ) { $entries = $this->memcacheConnection->get( 'gb-entries' ); if ( $entries !== false ) { return unserialize( $entries ); } } $sql = "SELECT * FROM guestbook;"; $stmt = $this->connection->prepare($sql); $stmt->execute(); $this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry'); } public function __destruct () { for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) { if ( empty ( $this -> entries[$i] -> id ) ) { $sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))"; $stmt = $this->connection->prepare($sql); $stmt->execute(array( $this->entries[$i]->author, $this->entries[$i]->email, $this->entries[$i]->website, $this->entries[$i]->message, $_SERVER['REMOTE_ADDR'] )); } } } } class GuestbookEntry { public $id; public $author; public $mail; public $website; public $message; public $date; public $ip; public function __set($key, $value) { if (isset($this->$key)) { $this->$key = $value; } } } ?> Lenke til kommentar
JohndoeMAKT Skrevet 21. desember 2008 Del Skrevet 21. desember 2008 La til blokk for innskriving i MC. <?php class Guestbook { private $numEntriesPerPage = 10; private $connection; private $entries; private $memcacheConnection; private $mcConnected; public function __construct(PDO $connection, $memcacheConnection) { $this->connection = $connection; $this->memcacheConnection = $memcacheConnection; } public function addEntry(GuestbookEntry $entry) { $this->entries[] = $entry; } public function fetchEntries(array $options = array()) { if ( $this->mcConnected ) { $entries = $this->memcacheConnection->get( 'gb-entries' ); if ( $entries !== false ) { return unserialize( $entries ); } } $sql = "SELECT * FROM guestbook;"; $stmt = $this->connection->prepare($sql); $stmt->execute(); $this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry'); if ( $this->mcConnected ) {} } public function __destruct () { for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) { if ( empty ( $this -> entries[$i] -> id ) ) { $sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))"; $stmt = $this->connection->prepare($sql); $stmt->execute(array( $this->entries[$i]->author, $this->entries[$i]->email, $this->entries[$i]->website, $this->entries[$i]->message, $_SERVER['REMOTE_ADDR'] )); } } } } class GuestbookEntry { public $id; public $author; public $mail; public $website; public $message; public $date; public $ip; public function __set($key, $value) { if (isset($this->$key)) { $this->$key = $value; } } } ?> Lenke til kommentar
Epower Skrevet 21. desember 2008 Del Skrevet 21. desember 2008 $entries = serialize($this->entries); <?php class Guestbook { private $numEntriesPerPage = 10; private $connection; private $entries; private $memcacheConnection; private $mcConnected; public function __construct(PDO $connection, $memcacheConnection) { $this->connection = $connection; $this->memcacheConnection = $memcacheConnection; } public function addEntry(GuestbookEntry $entry) { $this->entries[] = $entry; } public function fetchEntries(array $options = array()) { if ( $this->mcConnected ) { $entries = $this->memcacheConnection->get( 'gb-entries' ); if ( $entries !== false ) { return unserialize( $entries ); } } $sql = "SELECT * FROM guestbook;"; $stmt = $this->connection->prepare($sql); $stmt->execute(); $this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry'); if ( $this->mcConnected ) { $entries = serialize($this->entries); } } public function __destruct () { for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) { if ( empty ( $this -> entries[$i] -> id ) ) { $sql = "INSERT INTO entries (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))"; $stmt = $this->connection->prepare($sql); $stmt->execute(array( $this->entries[$i]->author, $this->entries[$i]->email, $this->entries[$i]->website, $this->entries[$i]->message, $_SERVER['REMOTE_ADDR'] )); } } } } class GuestbookEntry { public $id; public $author; public $mail; public $website; public $message; public $date; public $ip; public function __set($key, $value) { if (isset($this->$key)) { $this->$key = $value; } } } ?> Lenke til kommentar
Wackamole Skrevet 22. desember 2008 Del Skrevet 22. desember 2008 Released under the GPL/GNU license? Kommer til og blir bra når den er ferdig, hadde håpet på og bruke den selv *mine innlegg* Lenke til kommentar
Anbefalte innlegg
Opprett en konto eller logg inn for å kommentere
Du må være et medlem for å kunne skrive en kommentar
Opprett konto
Det er enkelt å melde seg inn for å starte en ny konto!
Start en kontoLogg inn
Har du allerede en konto? Logg inn her.
Logg inn nå