|
|
|
|
Strumenti |
15-03-2019, 22:02 | #1 |
Senior Member
Iscritto dal: Jan 2009
Città: SkyHeart
Messaggi: 3837
|
Chi mi spiega questo codice PHP?
Codice:
<?php /* * Template Class * Creates a template/view object */ class Template { //Path to template protected $template; //Variables passed in protected $vars = array(); /* * Class Constructor */ public function __construct($template){ $this->template = $template; } /* * Get template variables */ public function __get($key){ return $this->vars[$key]; } /* * Set template variables */ public function __set($key, $value){ $this->vars[$key] = $value; } /* * Convert Object To String */ public function __toString(){ extract($this->vars); chdir(dirname($this->template)); ob_start(); include basename($this->template); return ob_get_clean(); } } Codice:
// Ha la config con la connessione db e i parametri <?php require('core/init.php'); ?> <?php //Create Topic Object //E' tipo un model(non è MVC) dove mi creo un metodo per esempio per mostrare ciò che c'è nel database $topic = new Topic; //Get Template & Assign Vars //Passa al costruttore della classe template un path di una view $template = new Template('templates/frontpage.php'); //Assign Vars //assegna ad una variabile e al template la collection presa dal db $template->topics = $topic->getAllTopics(); //Display templates echo $template; ?>
__________________
Madre: Asus M4A785TD-V EVO; CPU: Amd Phenom X4 955BE Controlled by K10STAT; VGA: Gainward GTX260GS 896mb OC; HDD WD500AAKS; PSU: Corsair VX550W; RAM: Kinghstone hyper X 1600Mhz 1600mhz CL9 8Gb Reflex Canon Eos 6D + HELIOS44 M-6 50mm f2 + Canon 50mm f1.4 + Samyang 14mm f2.8 + Canon 28mm f2.8 IS Flickr Ultima modifica di Frenky-90 : 15-03-2019 alle 22:07. |
16-03-2019, 09:08 | #2 |
Senior Member
Iscritto dal: Jan 2008
Messaggi: 8406
|
In che senso come funzionano?
I metodi magici sono questi http://php.net/manual/en/language.oop5.magic.php E sono invocati dal runtime in momenti prestabiliti. I construttori quando si istanzia un nuovo oggetto di una classe. toString quando il contesto obbliga ad una conversione in stringa, ecc... Per esempio Codice:
echo $template; Codice:
echo $template->__toString(); |
16-03-2019, 12:42 | #3 |
Senior Member
Iscritto dal: Jan 2009
Città: SkyHeart
Messaggi: 3837
|
Si in grandi linee la sò..
Per esempio io passo questa collection tramite variabile $topics: $template->topics = $topic->getAllTopics(); cosa fà il metodo _get? organizzato in questo modo? Codice:
public function __get($key){ return $this->vars[$key]; } e poi il metodo __set()? Codice:
public function __set($key, $value){ $this->vars[$key] = $value; } extract del metodo toString trasforma in variabile la chiave array e come valore gli attribuisce il value dell'array? il chdir del metodo to string cambia la directori nel file dove si chiama in questo caso index.php? ob_start carica tutto nel buffer la include() ,inclde la view nel file index vero? ob_get_clean() recupare quello che c'è nel buffer e lo stampa Se hai un pò di tempo delucidami sulle domande poste. Grazie Saluti!
__________________
Madre: Asus M4A785TD-V EVO; CPU: Amd Phenom X4 955BE Controlled by K10STAT; VGA: Gainward GTX260GS 896mb OC; HDD WD500AAKS; PSU: Corsair VX550W; RAM: Kinghstone hyper X 1600Mhz 1600mhz CL9 8Gb Reflex Canon Eos 6D + HELIOS44 M-6 50mm f2 + Canon 50mm f1.4 + Samyang 14mm f2.8 + Canon 28mm f2.8 IS Flickr Ultima modifica di Frenky-90 : 16-03-2019 alle 12:53. |
16-03-2019, 14:00 | #4 | ||
Senior Member
Iscritto dal: Jan 2008
Messaggi: 8406
|
Quote:
Cioè nel tuo caso fa Codice:
$this->vars['topics'] = $value; // $value è il risultato di $topic->getAllTopics() Codice:
public function __get($key){ return $this->vars[$key]; } Codice:
$mia_variabile = $template->pippo; // notare che __get non compare da nessuna parte Codice:
$this->__get('pippo'); Quote:
Quello che fa extract è incredibile http://php.net/manual/en/function.extract.php Se guardi l'esempio nella pagina che ho linkato Codice:
$size = "large"; $var_array = array("color" => "blue", "size" => "medium", "shape" => "sphere"); extract($var_array, EXTR_PREFIX_SAME, "wddx"); echo "$color, $size, $shape, $wddx_size\n"; Perchè wddx_size? Perchè esiste già una variabile chiamata size. Poi chdir cambia la directory corrente. Basename estrae il nome del file da un path completo. Include include il file. ob_start --> attiva il buffering, cioè l'output non viene mandato immediatamente al client ob_get_clean --> cancella il buffer in cui è stato memorizzato l'output. Se volessi invece inviarlo, useresti ob_end_flush Perchè fa una cosa che sembra stupida? Perchè quando va ad includere l'altro file, potrebbero venire prodotti output indesiderati ( se ricordo bene pure i warnings finiscono nell'output ). Per evitare che al client venga inviata schifezza, semplicemente fa in modo che finisca in un buffer, che viene poi completamente cancellato, azzerato. Ultima modifica di pabloski : 16-03-2019 alle 14:07. |
||
16-03-2019, 14:45 | #5 |
Senior Member
Iscritto dal: Jan 2009
Città: SkyHeart
Messaggi: 3837
|
ecco un var_dump di $topics
Codice:
array(1) { [0]=> object(stdClass)#7 (10) { ["id"]=> string(1) "1" ["category_id"]=> string(1) "1" ["user_id"]=> string(1) "1" ["title"]=> string(23) "Php an awesome Language" ["body"]=> string(35) "Whit php you can do a lot of things" ["last_activity"]=> string(19) "0000-00-00 00:00:00" ["create_date"]=> string(19) "2019-03-13 12:56:04" ["username"]=> string(10) "pippo" ["avatar"]=> string(11) "avatar1.jpg" ["name"]=> string(15) "Web Programming" } Allora il primo metodo ad essere chiamato è __set Codice:
_set($topics, $iValoriDiTopics){ $this->vars[$topics] = $iValoriDiTopics; } e poi il metodo get__ __get($leChiaviArrayDiTopics){ return $this->vars[$leChiaviArrayDiTopics]; //che sono 'id', 'category_id' ecc }
__________________
Madre: Asus M4A785TD-V EVO; CPU: Amd Phenom X4 955BE Controlled by K10STAT; VGA: Gainward GTX260GS 896mb OC; HDD WD500AAKS; PSU: Corsair VX550W; RAM: Kinghstone hyper X 1600Mhz 1600mhz CL9 8Gb Reflex Canon Eos 6D + HELIOS44 M-6 50mm f2 + Canon 50mm f1.4 + Samyang 14mm f2.8 + Canon 28mm f2.8 IS Flickr |
16-03-2019, 17:08 | #6 |
Senior Member
Iscritto dal: Jan 2008
Messaggi: 8406
|
contiene un solo elemento?
comunque no, vars è un array associativo a cui vai ad aggiungere la chiave topics quando fai Codice:
$template->topics = $topic->getAllTopics(); quando fai Codice:
$miavar = $template->$topics; Codice:
$miavar = $template->__get('topics'); otterrai di nuovo tutto l'array...poi dovrai essere tu a scorrerlo ed accedere alle chiavi username,title,ecc... |
16-03-2019, 18:46 | #7 |
Senior Member
Iscritto dal: Jan 2009
Città: SkyHeart
Messaggi: 3837
|
Ho capito grazie..
No non è tutto quello l Array... Ho copiato solo il primo e ho modificato a mano per fare sembrare che e solo uno.. ..
__________________
Madre: Asus M4A785TD-V EVO; CPU: Amd Phenom X4 955BE Controlled by K10STAT; VGA: Gainward GTX260GS 896mb OC; HDD WD500AAKS; PSU: Corsair VX550W; RAM: Kinghstone hyper X 1600Mhz 1600mhz CL9 8Gb Reflex Canon Eos 6D + HELIOS44 M-6 50mm f2 + Canon 50mm f1.4 + Samyang 14mm f2.8 + Canon 28mm f2.8 IS Flickr |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 07:39.