|
20-maruza: phpda obyektga yo’naltirilgan dasturlash. Mvc texnologiyasi
|
bet | 8/9 | Sana | 22.11.2023 | Hajmi | 250,2 Kb. | | #103482 |
Bog'liq 15 Web ilova. MVC texnologiyasiInterfaces
interface Animal {
public function breath();
public function eat();
}
class Dog implements Animal{
//new method public function bark() {
return 'wof';
}
//implemented methods public function breath() {
return 'breathing';
}
public function eat() {
return 'eating';
}
}
$dog = new Dog();
echo $dog -> breath(); //breathing
echo $dog -> bark(); //wof
echo $dog -> eat();
//eating
- Obyektlar orasidagi munosabatlar
- Tarkibi PHPda muhim tushunchadir. Ushbu dizayn ko'rinishida ob'ekt boshqa ob'ektni yaratadi. Birlashtirish ob'ekt bir nechta ob'ektlardan iborat bo'lganda yuzaga keladi.
- Composition - bu egalik qilishga bo'lgan munosabat samarali, aggregation esa munosabatlarni "o'z ichiga oladi". Kompozitsiyada qismlar o'z ichiga olgan narsadan tashqarida mavjud bo'lolmaydi, lekin alohida narsalar birlashishda noyob birliklar sifatida mustaqil ravishda mavjud bo'lishi mumkin.
class Author {
private $name;
private $email;
public function __construct($name,$email){
$this->name = $name;
$this->email = $email;
}
public function getName(){
return $this->name;
}
public function getEmail(){
return $this->email;
}
}
class Book {
//using array to allow multiple authors
private $authors = array();
private $price;
private $name;
public function __construct($name,$price){
$this->name = $name;
$this->price = $price;
}
public function setPrice($price){
$this->price = $price;
}
public function getPrice(){
return $this->price;
}
public function getName(){
return $this->name;
}
public function addAuthor($name,$email){
$this->authors[] = new Author($name,$email);
}
public function getAuthors(){
return $this->authors;
}}
$book = new Book('Book Name',10.0);
$book -> addAuthor('Kelly','kelly@brainbell.com');
$book -> addAuthor('Ken','ken@brainbell.com'); /* print book info */
echo $book -> getName();
echo $book -> getPrice(); /* print book's authors info */
$authors = $book -> getAuthors(); //Array of authors
foreach ($authors as $author) {
echo $author -> getName();
echo $author -> getEmail();
}
|
| |