Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Utworzenie obiektu z parametrami
sadistic_son
post
Post #1





Grupa: Zarejestrowani
Postów: 1 495
Pomógł: 245
Dołączył: 1.07.2009
Skąd: Bydgoszcz

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


Cześć,
mam tu taki przykładzik z artykułu z lipca 2022 i nie rozumiem dlaczego autor podał parametry klasy przy tworzeniu obiektu. W artykule nie ma tego wyjaśnione, natomiast Visual Studio Code mówi mi przy tworzeniu obiektu, że klasa nie ma żadnego konstruktora, więc zostanie wywołana bez parametru. Ok, jasne, ale więc dlaczego podano te parametry?

klasa:
  1. <?php
  2.  
  3. class GermanShepherd
  4. {
  5. //...
  6.  
  7. private $eye_color;
  8. private $dob;
  9. private $does_shed = true;
  10. private $kingdom = "Animalia";
  11. private $phylum = "Chordata";
  12. private $class = "Mammalia";
  13. private $order = "Carnivara";
  14. private $family = "Canidae";
  15. private $genus = "Canis";
  16. private $species = "Canis Lupus";
  17. private $subspecies = "Canis Lupus Familiaris";
  18. private $breed = "German Shepherd Dog";
  19. private $fur_color;
  20.  
  21. //...
  22.  
  23. /**
  24.   * @return string
  25.   */
  26. public function getEyeColor(): string
  27. {
  28. return $this->eye_color;
  29. }
  30.  
  31. /**
  32.   * @param string $eye_color
  33.   */
  34. public function setEyeColor(string $eye_color): void
  35. {
  36. $this->eye_color = $eye_color;
  37. }
  38.  
  39. /**
  40.   * @return string
  41.   */
  42. public function getDob(): string
  43. {
  44. return $this->dob;
  45. }
  46.  
  47. /**
  48.   * @param string $dob
  49.   */
  50. public function setDob(string $dob): void
  51. {
  52. $this->dob = $dob;
  53. }
  54.  
  55. /**
  56.   * @return bool
  57.   */
  58. public function isDoesShed(): bool
  59. {
  60. return $this->does_shed;
  61. }
  62.  
  63. /**
  64.   * @param bool $does_shed
  65.   */
  66. public function setDoesShed(bool $does_shed): void
  67. {
  68. $this->does_shed = $does_shed;
  69. }
  70.  
  71. /**
  72.   * @return string
  73.   */
  74. public function getFurColor(): string
  75. {
  76. return $this->fur_color;
  77. }
  78.  
  79. /**
  80.   * @param string $fur_color
  81.   */
  82. public function setFurColor(string $fur_color): void
  83. {
  84. $this->fur_color = $fur_color;
  85. }
  86.  
  87. //...
  88. }


Wywołanie:
  1. $gs_dog = new GermanShepherd("Brown", "Jan 20, 2018", "Brown");
  2. echo $gs_dog->getEyeColor();
  3. $gs_dog->setEyeColor("Blue");
  4. echo $gs_dog->getEyeColor();


W ogóle kiedy to sie odpali to wywala błąd:
Kod
Fatal error: Uncaught TypeError: GermanShepherd::getEyeColor(): Return value must be of type string, null returned in C:\xampp\htdocs\index.php:28 Stack trace: #0 C:\xampp\htdocs\index.php(91): GermanShepherd->getEyeColor() #1 {main} thrown in C:\xampp\htdocs\index.php on line 28


Ten post edytował sadistic_son 12.01.2023, 11:34:03
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: 4.10.2025 - 10:58