Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Klasa w klasie
mablo
post
Post #1





Grupa: Zarejestrowani
Postów: 120
Pomógł: 1
Dołączył: 11.04.2005
Skąd: Poznań

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


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) (IMG:http://forum.php.pl/style_emoticons/default/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.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 8)
sobstel
post
Post #2





Grupa: Zarejestrowani
Postów: 853
Pomógł: 25
Dołączył: 27.08.2003
Skąd: Katowice

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


widze 2 sposoby

1. global $db
2. wzorzec singleton
Go to the top of the page
+Quote Post
nrm
post
Post #3





Grupa: Zarejestrowani
Postów: 627
Pomógł: 33
Dołączył: 1.05.2005
Skąd: Katowice

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


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. (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
Ociu
post
Post #4





Grupa: Moderatorzy
Postów: 1 566
Pomógł: 37
Dołączył: 14.05.2003
Skąd: Kraków




  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
Go to the top of the page
+Quote Post
mablo
post
Post #5





Grupa: Zarejestrowani
Postów: 120
Pomógł: 1
Dołączył: 11.04.2005
Skąd: Poznań

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


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 (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) poniewaz skrypt ma działać w php 4 i 5. A w 5 jest uławienie bo nie trzeba ładowć tej funkcji.
Go to the top of the page
+Quote Post
dr_bonzo
post
Post #6





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

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


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.

Ten post edytował dr_bonzo 5.12.2005, 19:01:30
Go to the top of the page
+Quote Post
mablo
post
Post #7





Grupa: Zarejestrowani
Postów: 120
Pomógł: 1
Dołączył: 11.04.2005
Skąd: Poznań

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


  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. ?>


Ten post edytował mablo 5.12.2005, 19:38:21
Go to the top of the page
+Quote Post
dr_bonzo
post
Post #8





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

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


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. ?>
Go to the top of the page
+Quote Post
mablo
post
Post #9





Grupa: Zarejestrowani
Postów: 120
Pomógł: 1
Dołączył: 11.04.2005
Skąd: Poznań

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


Dzieki już zrozumiałem oco chodzi i wszystko sobie poprawiłem i działa.
Go to the top of the page
+Quote Post

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: 22.08.2025 - 15:27