Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Wyświetlanie zawartości pliku poprzez klasę., Z include lub require
Tanner963
post
Post #1





Grupa: Zarejestrowani
Postów: 19
Pomógł: 1
Dołączył: 29.04.2010

Ostrzeżenie: (0%)
-----


Witam
Próbuję napisać metodę, która wyświetli/podepnie mi zawartość pliku jak include(itd.)
Fragment kodu:
  1. public function inc($path){
  2. include($path);
  3. }

Nie działa -> wyświetla białą stronę. Wyświetlanie błędów mam ustawione...
Próbowałem też z return, ale dalej to samo :|

Jak to rozwikłać? Z góry dzięki za pomoc (IMG:style_emoticons/default/wink.gif)
Pozdrawiam
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
abort
post
Post #2





Grupa: Zarejestrowani
Postów: 590
Pomógł: 107
Dołączył: 25.10.2011

Ostrzeżenie: (0%)
-----


Ja też od niedawna zgłębiam tajniki obiektowego pisania w PHP. Wcześniej conieco pisałem bezobiektowo, ale stwierdziłem, że nawet na starość warto nauczyć się czegoś nowego. Miałem podobny problem, zresztą nawet opisałem go na forum: Temat: PHP obiekty nie widza zmiennych z inkludowanych plikow
Założeniem jest składnia pliku konfiguracyjnego (u mnie config.php) w postaci tablicy:

  1. $config['sql']['host'] = "localhost";
  2. $config['sql']['user'] = "user";
  3. $config['sql']['pass'] = "password";
  4. $config['sql']['dbname'] = "database";
  5. $config['sql']['conn'] = "persistent";
  6. $config['sqltables']['login']['users'] = "users";
  7. $config['sqltables']['login']['logusers'] = "logusers";


No i zgodnie z założeniami naskrobałem... "coś". Nie jest może estetyczne, niemniej... działa tak jak tego oczekuję:

class.config.php:
  1. class Config {
  2. // array of config values
  3. private $config = array();
  4.  
  5. // Constructor - fetches all configuration data from cfg file
  6. function Config ($configfile="./config.php") {
  7. $handle=fopen ($configfile, "r");
  8. while (!feof ($handle)) {
  9. $line = fgets ($handle);
  10. if (preg_match ('/\s*\$(.*)\s+=\s+(.*)/', $line)) {
  11. $to_eval = preg_replace ('/\$/', '$this->', $line);
  12. eval ($to_eval);
  13. }
  14. }
  15. fclose ($handle);
  16. }
  17.  
  18. // returns first-level tree of config data. $tree must be string value
  19. function getTree ($tree) {
  20. if (is_string ($tree)) {
  21. return ($this->config ["$tree"]);
  22. } else return ($this->getSubtree ($this->config, $tree));
  23. }
  24.  
  25.  
  26. // walk deeper into subarray. Indexes specified in array
  27. function getSubtree ($subarray, $subtree) {
  28. if (is_array ($subtree)) {
  29. if (count ($subtree) == 1) {
  30. list ($subtreename) = $subtree;
  31. return ($subarray["$subtreename"]);
  32. }
  33. $first = array_shift ($subtree);
  34. return ($this->getSubtree ($subarray["$first"], $subtree));
  35. }
  36. }
  37.  
  38. } // class Config


A do samej klasy odwołuję się gdzie indziej w sposób:
  1. $cfg = new Config ();
  2.  
  3. $this->session_time = $session_lifetime;
  4. $this->menu_page = $menu_page;
  5. $this->login_page = $login_page;
  6. $this->sql_tables = $cfg->getTree (array ('sqltables', 'login')); // pobiera z config.php tablicę/wartość $config['sqltables']['login']
  7. $this->sql_config = $cfg->getTree ('sql'); // pobiera z config.php wartość/tablicę $config['sql'] -


Założeniem tego było, że modyfikacja pliku konfiguracyjnego jest dokonywane przez UPRAWNIONE osoby (są ustawione właściwe prawa w systemie) i aplikacja NIE ZAPISUJE nic do pliku konfiguracyjnego.

Fachowców proszę o skomentowanie tej klasy, widać nie tylko ja miałem taki problem z inkludowaniem w obiektach...
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 3.10.2025 - 08:41