Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]przemycanie parametru do destruktora
fiszol
post
Post #1





Grupa: Zarejestrowani
Postów: 452
Pomógł: 16
Dołączył: 25.05.2004
Skąd: Gorzów Wlkp.

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


Cześć.
Chcę napisać prostą klasę do operacji na plikach, w konstruktorze otwieram plik z żądanym parametrem i zwracam go w zmiennej (return $handle). W destruktor chciałem wsadzić fclose($handle), ale w deklaracji destruktora nie można podać żadnego parametru... Na chwilę obecną wygląda to tak:
  1. <?php
  2. class FileHandler{
  3. public $handle;
  4. function __consruct($file,$att = null)
  5. {
  6. if(file_exists($file) && is_readable($file))
  7. {
  8. switch($att)
  9. {
  10. default:
  11. $handle = fopen($file,"rb");
  12. break;
  13. case "write":
  14. $handle = fopen($file,"wb");
  15. break;
  16. }
  17. return $handle;
  18. }
  19. else
  20. {
  21. echo "File ".$file." does not exist or is unreadable";
  22. }
  23. }
  24. function write_file($file,$data)
  25. {
  26. if(!fwrite($handle,$data))
  27. {
  28. echo "Failed to write ".$data." into ".$file;
  29. }
  30. }
  31. function __destruct()
  32. {
  33. fclose($handle);
  34. }
  35. }
  36. ?>

Destruktor oczywiście wywala notice o niezdefiniowanej $handle oraz warninga o tym że fclose() nie ma co zamknąć.
Jak to rozwiązać?
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: 23.08.2025 - 12:01