Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Klasa w klasie
Forum PHP.pl > Forum > PHP
mablo
Odrazu mowie php 4.
Mam coś takiego
index.php:
  1. <?
  2. require("includes/initiate.php");
  3. /* Ładowanie modółów */
  4. include "pages/news.php";
  5. $news = new news();
  6. $news -> __construct();
  7. $news -> show();
  8. ?>

news.php:
  1. <?php
  2. class news
  3. {
  4.  
  5. var $limit;
  6.  
  7. function __construct()
  8. {
  9. $this -> limit = "10";
  10. }
  11.  
  12. function show ()
  13. {
  14.  
  15.  $db = new db();
  16. $db -> connect( DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASES);
  17. $Smarty = new Smarty();
  18. $Smarty -> template_dir = 'templates/';
  19. $Smarty -> compile_dir = 'templates_c/';
  20.  
  21. $s_SN = $db -> query ( "SELECT * FROM NEWS LIMIT ".$this -> limit.";" );
  22.  
  23. $newsy = array ();
  24. while ($row = $db -> fetch_assoc ($s_SN))
  25. $newsy[] = $row; 
  26.  
  27. $Smarty -> assign("news", $newsy);
  28. $Smarty -> display('news.tpl');
  29. }
  30. }
  31. ?>

initiate.php:
  1. <?php
  2. require( "libs/db.class.php" );
  3. require( "libs/Smarty.class.php" );
  4. require( "config.php" );
  5.  
  6. $db = new db();
  7. $db -> connect( DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASES);
  8.  
  9. $Smarty = new Smarty();
  10. $Smarty -> template_dir = 'templates/';
  11. $Smarty -> compile_dir = 'templates_c/';
  12. ?>


Czy moge jakoś ominąc odpalanie klas db i smarty w funkcji show (news.php) questionmark.gif Żeby odpalały się w pliku initiate.php bo dodawanie odpalania klas smarty i db za każdym razem jest troche uciążliwe.
sobstel
widze 2 sposoby

1. global $db
2. wzorzec singleton
nrm
1. swoją droga piszesz php4 a podajesz __construct(); ?!?

2. możesz przekazać odpalona klase db do klasy news

np.

$news = new news(&$db);

a w klasie news w konstruktorze (w php4 o nazwie jak klasa)
robisz $this->db = &$db;

tak się kiedyś nauczyłem i tak mi zostało. nie wiem czy to jest prawidłowe postepowanie. smile.gif
Ociu
  1. <?php
  2. # php5
  3. public function __contruct(db $db) {
  4. $this->db = $db;
  5. }
  6.  
  7. # php4
  8.  
  9. function news(&$db) {
  10. $this->db = $db;
  11. }
  12.  
  13. #index.php
  14. $news = new news(new db());
  15. ?>

pozdrawiam
mablo
Nie wiem co robie źle ale nie działa.
Cytat
Warning: Call-time pass-by-reference has been deprecated - argument passed by value; If you would like to pass it by reference, modify the declaration of [runtime function name](). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file. However, future versions may not support this any longer. in C:\php\www\cms\news\index.php on line 28


Czemu __construct questionmark.gif poniewaz skrypt ma działać w php 4 i 5. A w 5 jest uławienie bo nie trzeba ładowć tej funkcji.
dr_bonzo
Uzyj standardowego rozwiazania: http://pl.php.net/manual/en/language.oop5.decon.php
Cytat
For backwards compatibility, if php 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics.

---
Co do bledu -- ktora to linia?, i podaj naglowek funkcji ktora wywolujesz.
mablo
  1. <?php
  2. $news = new news(&$db);
  3. ?>
to jest linia 28.
A do news.php dodałem
  1. <?php
  2. function __construct()
  3. {
  4. $this -> limit = "10";
  5. $this -> db = &$db;
  6. }
  7. ?>
dr_bonzo
Zeby uproscic sprawe (wszystkie bledy poprawie od razu):
  1. <?php
  2.  
  3. class news
  4. {
  5. var $limit;
  6. var $db;
  7. function news( &$db ) // tutaj dajesz '&' a nie przy wywplaniu
  8. /*
  9. * ten sposob tworzenia konstruktora jest zgodny z php4 i php5
  10. i zrozumialy dla wszystkich ktorzy przeczytali ten rozdzial manuala
  11. gdy robisz:
  12.  
  13. $news = new news();
  14. $news->__construct();
  15.  
  16. nie dosc ze dziwnie wyglada to jest niezrozumiale
  17.  
  18.  
  19. * musisz podac argumenty funkcji, a nie taK:
  20. function __construct()
  21. {
  22. $this->limit = "10";
  23. $this->db = &$db; // skad wzialzes $db
  24. }
  25. */
  26. {
  27. $this -> limit = "10";
  28. $this->db = &$db;
  29. }
  30. }
  31.  
  32.  
  33. $news = new news($db); 
  34. // ^^^ For backwards compatibility...
  35. // tu nie podajesz '&' tlyko sama zmienna, & dodajesz w definicji funkcji
  36. ?>
mablo
Dzieki już zrozumiałem oco chodzi i wszystko sobie poprawiłem i działa.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.