Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] OOP, Object-Oriented Programming
kosmos
post
Post #1





Grupa: Zarejestrowani
Postów: 367
Pomógł: 17
Dołączył: 4.03.2008

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


Witam

Już kiedyś pisałem że chciałem się wdrożyć w programowanie obiektowe w PHP (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

Napisałem cosbie taki oto kod składający się z 3 klas:

  1. <?php
  2.  
  3. class Car{
  4.  protected $mark,$color,$mileage;
  5.  
  6.  protected function __construct($mark,$color,$milleage){
  7.  $this->mark=$mark;
  8. $this->color=$color;
  9. $this->mileage=$milleage;
  10.  }
  11.  
  12.  protected function getMark(){
  13.  return $this->mark;
  14. }
  15.  
  16.  protected function getColor(){
  17.  return $this->color;
  18. }
  19.  
  20.  protected function getMileage(){
  21.  return $this->mileage;
  22.  }
  23.  
  24.  private function getAllPropertiesOfCar(){
  25.  return $this->getMark().' '.$this->getColor().' '.$this->getMileage();
  26.  }
  27.  
  28.  }
  29.  
  30.  
  31. class Person extends Car{
  32.  protected $name,$surname,$age,$profession,$auto;
  33.  
  34.  protected function setName($name){
  35.  $this->name=$name;
  36.  }
  37.  
  38.  protected function setSurname($surname){
  39.  $this->surname=$surname;
  40. }
  41.  
  42.  protected function setAgeAndProfession($age,$profession){
  43.  $this->age=$age; $this->profession=$profession;
  44.  }
  45.  
  46.  protected function giveAllPropertiesOfPerson(){
  47.  print $this->name.' '.$this->surname.' '.$this->age.' '.$this->profession.'<br><br>';
  48.  
  49.  }
  50.  
  51.  
  52.  public  function chooseYourCar($mark,$color,$milleage){
  53.  $this->mark=$mark;
  54.  $this->color=$color;
  55.  $this->mileage=$milleage;
  56.  }
  57.  
  58.  
  59.  
  60.  
  61. public  function showNewCar(){
  62.  print $this->name. ' bought a new car. It`s: '.$this->getMark().' the color is: '.$this->getColor().' and the milleage is not to bad: '.$this->getMileage();
  63.  }
  64.  
  65.  }
  66.  
  67.  
  68.  
  69.  class Family extends Person{
  70.  private $address,$fortune,$volume;
  71.  
  72.  function __construct($address,$fortune,$volume){
  73.  
  74. $this->address=$address;
  75.  $this->fortune=$fortune;
  76.  $this->volume=$volume;
  77.  }
  78.  
  79.  
  80.  public function addPerson($name,$surname,$age,$profession){
  81.  $this->setName($name);
  82. $this->setSurname($surname);
  83. $this->setAgeAndProfession($age,$profession);
  84. }
  85.  
  86.  
  87.  public function printFamily(){
  88. $this->giveAllPropertiesOfPerson();
  89. }
  90.  
  91.  public function printPropertiesOfFamilyCar(){
  92. $this->showNewCar();
  93. }
  94.  
  95.  
  96.  public function hugePersonOfFamily(Person $p){
  97.  print'<br>';
  98.  return $this->name.' is hugging '. $p->name;
  99.  }
  100.  
  101.  };
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  // CIAŁO
  112.  
  113.  $family= new Family('London',240000,4);
  114.  $family->addPerson('John','Kowalski',46,'Programmer');
  115.  
  116.  $family_a=new Family('Krakow',120000,2);
  117.  $family_a->addPerson('Izabel','Kowlaska',44,'Designer');
  118.  
  119.  $family->printFamily();
  120.  $family_a->printFamily();
  121.  
  122.  $family->chooseYourCar('volvo','red' ,230000 );
  123.  $family->showNewCar();
  124.  print $family->hugePersonOfFamily($family_a);
  125.  
  126. ?>

Proszę znawców o zweryfikowanie czy dobrze rozumiem pojecie obiektowości. Zastosowałem oddzielne klasy dla obiektów, dodałem dla nich unikalne funkcje gdzie niekiedy inne obiekty korzystają z funkcji innych obiektów.

Proszę o opinie czy moje wypocinki można nazwać choź troszkę OOP (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)

EDIT:

oto wynik zwracany przez powyższy kod

Kod
John Kowalski 46 Programmer

Izabel Kowlaska 44 Designer

John bought a new car. It`s: volvo the color is: red and the milleage is not to bad: 230000
John is hugging Izabel


Ten post edytował kosmos 30.05.2008, 20:30:47
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
kosmos
post
Post #2





Grupa: Zarejestrowani
Postów: 367
Pomógł: 17
Dołączył: 4.03.2008

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


Witam ponownie

@dr_bonzo

napisałem to co poleciłeś poszło całkiem dobrze już chyba pomalutku zaczynam rozumieć OOP.


  1. <?php
  2.  
  3. class Family{
  4. private $city,$fortune,$volume;
  5. public $person;
  6. public $car;
  7.  
  8. function __construct($city,$fortune,$volume){
  9. $this->city=$city;
  10. $this->fortune=$fortune;
  11. $this->volume=$volume;
  12. $this->person=array();
  13. }
  14.  
  15. public function addPerson($person){
  16. $this->person[]=$person;
  17. }
  18.  
  19. public function addCar($car){
  20. $this->person[]=$car;
  21. }
  22.  
  23. public function printFamily(){
  24. /**  foreach($this->person as $key => $value)
  25. print $this->person[$key] -> name;
  26. print $this->person[$key] -> surname;
  27. print $this->person[$key] -> age;
  28. print $this->person[$key] -> occupation;
  29. print '<br>';
  30. print_r($this->person);
  31.  } NIESTETY NIE DZIAŁA POPRAWNIE :(*/
  32. for($int = 0; $int < count($this->person); $int++){
  33. print $this->person[$int]->name .'<br>';
  34. print $this->person[$int]->surname .'<br>' ;
  35. print $this->person[$int]->age .'<br>';
  36. print $this->person[$int]->occupation .'<br><br>';
  37. }
  38.  }
  39. };
  40.  
  41. class Person{
  42. public $name,$surname,$age;
  43. public $occupation;
  44.  
  45. function __construct($name,$surname,$age){
  46. $this->name=$name;
  47. $this->surname=$surname;
  48. $this->age=$age;
  49. }
  50.  
  51. public function setOccupation($occupation){
  52. $this->occupation=$occupation;
  53.  }
  54.  
  55. public function huge(Person $p){
  56. print $this->name.' is hugging '.$p->name;
  57. }
  58. };
  59.  
  60. class Car{
  61. private $mark,$color,$mileage;
  62.  
  63. public function __construct($mark,$color,$milleage){
  64.  $this->mark=$mark;
  65. $this->color=$color;
  66. $this->mileage=$milleage;
  67. }
  68.  
  69. public function printLatestCarInfo(){
  70. print $this->mark.' '.$this->color.' '.$this->mileage;
  71. }
  72. };
  73.  
  74. // CIAŁO
  75. $family= new Family('London','240000',2);
  76. $john= new Person('John','Kowalski',46);
  77. $john->setOccupation('Programmer');
  78. $bob= new Person('Bob','Kowalski',10);
  79. $family->addPerson($john);
  80. $family->addPerson($bob);
  81.  
  82. $family_a= new Family('Krakow','120000',1);
  83. $iza= new Person('Izabela','Nowak',44);
  84. $iza->setOccupation('Designer');
  85. $family_a->addPerson($iza);
  86.  
  87. $family->printFamily();
  88. print'Wydruk drugiej rodzinki';
  89. $family_a->printFamily();
  90.  
  91. $family->addCar($car=new Car('Volvo','red',230000));
  92. print'Wydruk samochodu rodzinki<br>';
  93. $car->printLatestCarInfo();
  94. print'<br>Ściskają się<br>';
  95. $john->huge($iza);
  96. ?>


WYNIK

Kod
John
Kowalski
46
Programmer

Bob
Kowalski
10


Wydruk  drugiej rodzinkiIzabela
Nowak
44
Designer

Wydruk samochodu  rodzinki
Volvo red 230000
Ściskają się
John is hugging Izabela


Może jakiś kolejny przykład (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)


@ NOSPOR

Cytat(nospor @ 30.05.2008, 23:32:09 ) *
Pomijam już fakt, że $name to z kosmosu bierzesz.
Moze zamiast za OOP to weź się najpierw za podstawy php?



uznam to za złośliwość z Twojej strony ( wczoraj było dosyć późno ... nie każdy jest robokopem a każdy potrzenuje wypoczynku stąd być może moje błędy )

Ten post edytował kosmos 31.05.2008, 14:37:26
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: 6.10.2025 - 16:15