Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> opcjonalne argumenty w funkcjach, jak dodawać opcjonalne argumenty?
jogurt666
post
Post #1





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 3.07.2004
Skąd: brak danych

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


pracuję nad klasą wspomagającą pracę z bazą danych MySQL. początek klasy wygląda tak:


  1. <?php
  2.  
  3. class db
  4. {
  5. var $host = &#092;"localhost\";
  6. var $db = &#092;"sample\";
  7.  
  8. function db()
  9. {
  10. $args = func_get_args();
  11. if($args[0] != $this->db && !empty($args[0])) $this->db = $args[0];
  12. if($args[1] != $this->host && !empty($args[1])) $this->host = $args[1];
  13. }
  14.  
  15. //reszta metod
  16. }
  17.  
  18. ?>

jak widać metoda db ma być wywołana podczas tworzenia obiektu i ma przyjmować 2 opcjonalne argumenty. tutaj poradziłem sobie z tym używając funkcji func_get_args(), w razie kiedy któryś z argumentów nie zostanie przekazany, metoda użyje wartości domyślnych dla tych argumentów.

metoda niby załatwia sprawę opcjonalnych argumentów, ale jest to zrobione "na dziko". czy jest jakiś lepszy sposób na przekazywanie opcjonalnych argumentów dla funkcji/metod (abstrachuję tutaj od global)?

Ten post edytował jogurt666 9.07.2004, 16:56:31
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 4)
Yarecki
post
Post #2





Grupa: Zarejestrowani
Postów: 166
Pomógł: 0
Dołączył: 1.04.2004
Skąd: Gdynia

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


np. można tak:
  1. <?
  2. class Db
  3. {
  4. var $host = &#092;"localhost\";
  5. var $db = &#092;"sample\";
  6.  
  7. function db( $args )
  8. {
  9. if($args[0] != $this->db && !empty($args[0])) $this->db = $args[0];
  10. if($args[1] != $this->host && !empty($args[1])) $this->host = $args[1];
  11. }
  12.  
  13. //reszta metod
  14. }
  15. ?>

i wywołanie wygląda tak:
  1. <?php
  2.  
  3. $arrArgs = array( 'inny_host', 'inna_db' );
  4. $objDb = &new Db( $arrArgs );
  5.  
  6. ?>
Go to the top of the page
+Quote Post
mls
post
Post #3





Grupa: Zarejestrowani
Postów: 677
Pomógł: 89
Dołączył: 31.08.2003
Skąd: Warszawa

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


Hmm...
  1. <?php
  2. class db
  3. {
  4.  var $host;
  5.  var $db;
  6.  
  7.  function db($db = 'sample', $host = 'localhost')
  8. {
  9.  $this->db = $db;
  10.  $this->host = $host;
  11. }
  12.  
  13. //reszta metod
  14. }
  15. ?>


Czy źle zrozumiałem?
Go to the top of the page
+Quote Post
Amao
post
Post #4





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 13.03.2004
Skąd: Jaworzno

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


Można też tak :
  1. <?
  2. class Db
  3. {
  4. var $host = &#092;"localhost\";
  5. var $db = &#092;"sample\";
  6.  
  7. function db($db=false,$host=false)
  8. {
  9.  
  10. if($db)
  11. {
  12. $this->db = $db;
  13. }
  14. if($host)
  15. {
  16. $this->host = $host;
  17. }
  18.  
  19. }
  20.  
  21. }
  22. ?>
Go to the top of the page
+Quote Post
Jabol
post
Post #5





Grupa: Przyjaciele php.pl
Postów: 1 467
Pomógł: 13
Dołączył: 22.02.2003

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


a nie łatwiej?
  1. <?php
  2. class db
  3. {
  4. var $db=&#092;"default\", $host=\"default\";
  5. function _set_db($db=null)
  6. {
  7. if($db) $this->db=$db;
  8. }
  9. function _set_host($host=null)
  10. {
  11. if($host) $this->host=$host;
  12. }
  13. function db($db=null, $host=null)
  14. {
  15. $this->_set_db($db);
  16. $this->_set_host($host);
  17. }
  18. }
  19. $db=new db();
  20. $db=new db(&#092;"nie_default\");
  21. $db=new db(&#092;"nie_default\", null);
  22. $db=new db(null, &#092;"nie_default\");
  23. $db=new db(&#092;"nie_default\", \"nie_default\");
  24. $db=new db; $db->_set_host(&#092;"nie_default\");
  25. // i tak dalej
  26. ?>
rozwiązanie podobne do Amao, ale lubiem jak można coś robić ręcznie i np. zmieniać sobie potem.
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: 25.08.2025 - 20:55