Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> instancje
M4chu
post 3.08.2004, 12:18:17
Post #1





Grupa: Zarejestrowani
Postów: 135
Pomógł: 0
Dołączył: 28.09.2003
Skąd: Rzeszów

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


Witam, pisze sobie klase do obslugi wielu baz danych np DbMySql, DbPgSql itp, wybor bazy bedzie okreslany w konstruktorze np
  1. <?php
  2.  
  3. $db = new Db('MySql');
  4.  
  5. ?>

problem w tym, ze nie wiem jak w klasie Db kozystac ze zmiennych i metod klasy
  1. <?php
  2.  
  3. class Db
  4. {
  5. function __construct($type)
  6. {
  7. $this->type = $type;
  8. require_once $type.'/class.php';
  9. // i teraz nie wiem jak zrobic, zeby klasa Db miala wszystkie metody i zmienne kla
  10. $type
  11. // cos w stylu $this = $type::getInstance();
  12. }
  13. }
  14.  
  15. ?>

Pewnie zalatwia to jedna linijka kodu smile.gif
pozdrawiam
Go to the top of the page
+Quote Post
ksiadz
post 3.08.2004, 12:43:59
Post #2





Grupa: Zarejestrowani
Postów: 39
Pomógł: 0
Dołączył: 24.11.2003

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


do juz istniejacej klasy nie mozesz przypisac mowych metod - ta klasa moze tylko odziedziczyc funkcje z wczesniej zdefiniowanej ale mozna to podejsc inaczej
  1. <?php
  2.  
  3. class DB
  4. {
  5.  
  6. public $db_functions;
  7.  
  8. function __construct($type)
  9. {
  10. $this->type = $type;
  11. require_once($type.'/class.php');
  12.  
  13. $this->db_functions = new $type;
  14. }
  15. }
  16.  
  17. ?>

a odwolac sie mozesz do funkcji w ten sposob
  1. <?php
  2.  
  3. $DB->db_functions->Query();
  4.  
  5. ?>

Chyba ze jest jakis sposob o ktorym nie mam pojecia smile.gif


--------------------
If you can't find a program that does what you want it to do, then write your own.
Go to the top of the page
+Quote Post
M4chu
post 3.08.2004, 13:03:20
Post #3





Grupa: Zarejestrowani
Postów: 135
Pomógł: 0
Dołączył: 28.09.2003
Skąd: Rzeszów

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


O tym sposobie wiem, jesli nie ma innego to pewnie bede musial sie nim zadowolic tongue.gif
Go to the top of the page
+Quote Post
ksiadz
post 6.08.2004, 16:49:32
Post #4





Grupa: Zarejestrowani
Postów: 39
Pomógł: 0
Dołączył: 24.11.2003

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


chyba zeby zastosowac pewien chwyt
  1. <?php
  2.  
  3. class DB
  4. {
  5.  
  6. public $db_functions;
  7.  
  8. function __construct($type)
  9. {
  10. $this->type = $type;
  11. require_once($type.'/class.php');
  12.  
  13. $this->db_functions = new $type;
  14. }
  15.  
  16. function __call( $function, $arg )
  17. {
  18.  $this->db_functions->$function( implode( ',', $arg ) );
  19. }
  20. }
  21.  
  22. ?>

nie wiem czy dziala bo pisze "z palca" winksmiley.jpg


--------------------
If you can't find a program that does what you want it to do, then write your own.
Go to the top of the page
+Quote Post
M4chu
post 6.08.2004, 16:56:18
Post #5





Grupa: Zarejestrowani
Postów: 135
Pomógł: 0
Dołączył: 28.09.2003
Skąd: Rzeszów

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


Tak wlasnie o to mi chodzilo, wielkie dzieki smile.gif
Go to the top of the page
+Quote Post
bregovic
post 10.08.2004, 22:40:14
Post #6





Grupa: Zarejestrowani
Postów: 562
Pomógł: 15
Dołączył: 8.08.2003
Skąd: Denmark/Odense

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


Jest tylko jeden drobny problem... mianowicie jesli jeden z argumentow przekazanych do __call jest tablica - to implode( ',', $arg ) niestety nie przejdzie... glowilem sie nad tym przez ostatnie pol godziny, ale nie moge znalezc lepszego wyjscia niz
  1. <?php
  2. function __call($function, $arg)
  3. {
  4. list($a[0], $a[1], $a[2], $a[3], $a[4]) = $arg;
  5. $this->db_functions->$function($a[0], $a[1], $a[2], $a[3], $a[4]);
  6. }
  7. ?>

Ale to jest koszmarne... Niestety nie mozna uzyc call_user_func_array" title="Zobacz w manualu PHP" target="_manual - a przynajmniej ja nie porafie uzyc tego na obiekcie. Znalazlem za to call_user_method_array" title="Zobacz w manualu PHP" target="_manual - i to jest to!
  1. <?php
  2. function __call($function, $arg)
  3. {
  4. call_user_method_array($function, $this->db_functions, $arg);
  5. }
  6. ?>


--------------------
Prank - for the fun. Mac - for the simplicity. Deviantart - for the kick.
Life is ours, We live it our way -- Metallica
Go to the top of the page
+Quote Post
davidD
post 10.08.2004, 23:26:46
Post #7





Grupa: Zarejestrowani
Postów: 13
Pomógł: 0
Dołączył: 10.10.2002

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


Cytat(bregovic @ 2004-08-10 22:40:14)
[...] Znalazlem za to call_user_method_array" target="_blank - i to jest to!
  1. <?php
  2. function __call($function, $arg)
  3. {
  4. call_user_method_array($function, $this->db_functions, $arg);
  5. }
  6. ?>

To już chyba lepiej zrobić tak:

  1. <?php
  2. function __call($function, $arg)
  3. {
  4.  call_user_func_array(array(&$this->db_functions, $function), $arg);
  5. }
  6. ?>


Zalecają właśnie ten sposób przy opisie call_user_method_array

Pozdrawiam smile.gif


--------------------
| WinXP | Apache 2.0.50 | php 5.0.3 | MySQL 4.0.20 | Zend Studio |
CMS::engine llllllllllllllllllll 80%
CMS::userInterface
Go to the top of the page
+Quote Post
bregovic
post 10.08.2004, 23:32:00
Post #8





Grupa: Zarejestrowani
Postów: 562
Pomógł: 15
Dołączył: 8.08.2003
Skąd: Denmark/Odense

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


Ups! To sie nazywa slepota - nie zauwazyc WARNINGa ;]


--------------------
Prank - for the fun. Mac - for the simplicity. Deviantart - for the kick.
Life is ours, We live it our way -- Metallica
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 15.06.2025 - 05:40