passaggio da procedurale a oop
-
Buongiorno a tutti,
sono un appassionato dilettante di php, quel poco che ho fatto l'ho scritto sempre con php procedurale,
ora vorrei passare a php ad oggetti, per questo vorrei una delucidazione:prima scrivevo:
function utenti(){ $array_utenti = array(); $query_utenti = mysql_query("SELECT utenti.nome, utenti.cognome, utenti.foto, utenti.email, siti.url FROM utenti INNER JOIN utente_sito ON utenti.id = utente_sito.utente INNER JOIN siti ON siti.id = utente_sito.sito ORDER BY siti.url ASC"); while ($row = mysql_fetch_assoc($query_utenti)): $array_utenti[] = array( "nome" => $row['nome'], "cognome" => $row['cognome'], "foto" => $row['foto'], "email" => $row['email'], "url" => $row['url'] ); endwhile; $utenti = array(); foreach ($array_utenti as $utente) { $utenti[] = array( "nome" => $utente['nome'], "cognome" => $utente['cognome'], "foto" => $utente['foto'], "email" => $utente['email'], "url" => $utente['url'] ); } return $utenti; } $utenti = utenti(); foreach ($utenti as $utente) { echo $utente['url']."<br />"; echo $utente['nome']."<br />"; echo $utente['cognome']."<br />"; echo $utente['foto']."<br />"; echo $utente['email']."<hr>"; }ora scrivo:
class Editor{ public function utenti(){ $array_utenti = array(); $query_utenti = mysql_query("SELECT utenti.nome, utenti.cognome, utenti.foto, utenti.email, siti.url FROM utenti INNER JOIN utente_sito ON utenti.id = utente_sito.utente INNER JOIN siti ON siti.id = utente_sito.sito ORDER BY siti.url ASC"); while ($row = mysql_fetch_assoc($query_utenti)): $array_utenti[] = array( "nome" => $row['nome'], "cognome" => $row['cognome'], "foto" => $row['foto'], "email" => $row['email'], "url" => $row['url'] ); endwhile; $utenti = array(); foreach ($array_utenti as $utente) { $utenti[] = array( "nome" => $utente['nome'], "cognome" => $utente['cognome'], "foto" => $utente['foto'], "email" => $utente['email'], "url" => $utente['url'] ); } return $utenti; } } $class_editor = new Editor(); $utenti = $class_editor->utenti(); foreach ($utenti as $utente) { echo $utente['url']."<br />"; echo $utente['nome']."<br />"; echo $utente['cognome']."<br />"; echo $utente['foto']."<br />"; echo $utente['email']."<hr>"; }secondo voi è corretto?
-
class Editor { private $db; protected $array_utenti = array(); public function __construct() { $this->db = new mysqli('localhost', 'root', '', 'prova'); } protected function utenti() { $query_utenti = $this->db->query('SELECT utenti.nome, utenti.cognome, utenti.foto, utenti.email, siti.url FROM utenti INNER JOIN utente_sito ON utenti.id = utente_sito.utente INNER JOIN siti ON siti.id = utente_sito.sito ORDER BY siti.url ASC'); while ($row = $query_utenti->fetch_array(MYSQLI_ASSOC)) { $this->array_utenti[] = array( "nome" => $row['nome'], "cognome" => $row['cognome'], "foto" => $row['foto'], "email" => $row['email'], "url" => $row['url'] ); } } public function getUtenti() { $this->utenti(); print "<pre>".print_r($this->array_utenti, true)."</pre>"; foreach($this->array_utenti as $utente) { echo $utente['url']."<br />"; echo $utente['nome']."<br />"; echo $utente['cognome']."<br />"; echo $utente['foto']."<br />"; echo $utente['email']."<hr>"; } } } $editor = new Editor(); $editor->getUtenti(); -
Grazie mille x la risposta, scusa il ritardo ma non ho ricevuto la notifica. Do un occhio al codice e se ho dubbi ti chiedo. Buon week end e grazie ancora!
-
Ciao Jhon92,
ho una domanda, ma perchè estrai i dati con un metodo, e con un altro li stampi? Sicurezza?sto provando a scrivere delle classi, posto il codice così mi dici cosa ne pensei.
ciao e grazie!
<?php class Examples{ private $db; public function __construct(){ $this->db = new mysqli('localhost', 'root', 'hal9000', 'examples'); } public function get_actors(){ $actors = array(); $query = $this->db->query('SELECT * FROM actor LIMIT 10'); while ($row = $query->fetch_array(MYSQLI_ASSOC)){ $actors[] = array( "id" => $row['actor_id'], "fullname" => $row['fullname'] ); } return $actors; } public function __destruct(){ mysqli_close($this->db); } } class Codeigniter{ private $db; public function __construct(){ $this->db = new mysqli('localhost', 'root', 'hal9000', 'myapp_moto'); } public function get_actors(){ $actors = array(); $query = $this->db->query('SELECT * FROM articoli LIMIT 10'); while ($row = $query->fetch_array(MYSQLI_ASSOC)){ $actors[] = array( "id" => $row['id'], "titolo" => $row['titolo'] ); } return $actors; } public function __destruct(){ mysqli_close($this->db); } } $attori = new Examples(); echo "<pre>"; print_r($attori->get_actors()); echo "</pre>"; $news = new Codeigniter(); echo "<pre>"; print_r($news->get_actors()); echo "</pre>"; ?> -
-
Perchè?
Ciao! Sembra che tu sia interessato a questa conversazione, ma non hai ancora un account.
Stanco di dover scorrere gli stessi post a ogni visita? Quando registri un account, tornerai sempre esattamente dove eri rimasto e potrai scegliere di essere avvisato delle nuove risposte (tramite email o notifica push). Potrai anche salvare segnalibri e votare i post per mostrare il tuo apprezzamento agli altri membri della comunità.
Con il tuo contributo, questo post potrebbe essere ancora migliore 💗
Registrati Accedi