Advertisement
sanjiisan

Untitled

Apr 10th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. <?php
  2.  
  3. class File
  4. {
  5. protected $path; //Atrybuty protected - niedostępne spoza klasy i widoczne w klasie dziedziczącej
  6. protected $size;
  7.  
  8. public function getPath() //Getter do zmiennej path
  9. {
  10. return $this->path;
  11. }
  12.  
  13. public function getSize() //Getter do zmiennej size
  14. {
  15. return $this->size;
  16. }
  17.  
  18. public function setSize($size)//Size setter - sprawdzamy czy dane sa poprawne potem je ustawiamy
  19. {
  20. if ($size < 0 || !is_numeric($size)) {
  21. return false;
  22. }
  23.  
  24. $this->size = $size;
  25. }
  26.  
  27. public function setPath($path)//Path setter - sprawdzamy czy dane sa poprawne potem je ustawiamy
  28. {
  29. if (!is_string($path) || $path[0] !== '/') {
  30. return false;
  31. }
  32.  
  33. $this->path = $path;
  34. }
  35.  
  36. public function __construct($path, $size) //Konstruktor przyjmujące dwa parametry
  37. {
  38. if (
  39. !is_string($path) ||
  40. $path[0] !== '/' ||
  41. $size < 0 ||
  42. !is_numeric($size)
  43. ) {
  44. return false;
  45. }
  46.  
  47. $this->size = $size;
  48. $this->path = $path;
  49. }
  50.  
  51. public function calculateSize($unit)
  52. {
  53. switch ($unit) {
  54. case 'MB': {
  55. $result = floor($this->size / 1024 / 1024);
  56. break;
  57. }
  58. case 'KB': {
  59. $result = floor($this->size / 1024);
  60. break;
  61. }
  62. default: {
  63. echo 'Zły format!';
  64. return false;
  65. }
  66. }
  67.  
  68. return $result;
  69. }
  70. }
  71.  
  72. $wmpFile = new File('/asdasd', 156489789); //Stworzenie obiektu typu file
  73.  
  74. echo $wmpFile->calculateSize('MB'); //Metoda użyta(zmiena jednostek)
  75.  
  76. var_dump($wmpFile); //pokazanie
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement