├── 01-base-class.php ├── 02-properties.php ├── 03-methods.php ├── 04-constructors.php ├── 05-inheritance.php ├── 06-static.php ├── 07-override.php ├── 08-abstract-class.php ├── 09-interface.php └── README.md /01-base-class.php: -------------------------------------------------------------------------------- 1 | authorName = 'Bill Bryson'; 19 | $book001->genre = 'Non-fiction'; 20 | $book001->yearPublished = 2003; 21 | $book001->title = 'A Short History of Nearly Everything'; 22 | $book001->noPages = 544; 23 | 24 | 25 | // View output 26 | print_r($book001); 27 | echo "\n"; 28 | var_dump($book001); 29 | echo "\n"; 30 | 31 | -------------------------------------------------------------------------------- /03-methods.php: -------------------------------------------------------------------------------- 1 | genre = strtolower($this->genre); 17 | return "This $this->genre title, $this->title, was published by $this->authorName in $this->yearPublished."; 18 | } 19 | 20 | // set rating as float out of 5 21 | public function setRating(float $ratingOutOf5) 22 | { 23 | $this->bookRating = $ratingOutOf5; 24 | } 25 | 26 | // get private property $bookRating 27 | public function getRating() 28 | { 29 | return "GETTER retrieves Rating: $this->bookRating"; 30 | } 31 | 32 | // get protected string $isbn 33 | public function getISBN() 34 | { 35 | return $this->isbn; 36 | } 37 | } 38 | 39 | 40 | $book001 = new Book(); 41 | $book001->title = 'A Short History of Nearly Everything'; 42 | $book001->authorName = 'Bill Bryson'; 43 | $book001->genre = 'Non-fiction'; 44 | $book001->yearPublished = 2003; 45 | $book001->noPages = 544; 46 | 47 | // Set value for book rating and isbn 48 | $book001->setRating(4.5); 49 | 50 | // View output for METHODS 51 | echo $book001->getBookInfo(); 52 | echo "\n"; 53 | echo $book001->getRating(); 54 | 55 | 56 | -------------------------------------------------------------------------------- /04-constructors.php: -------------------------------------------------------------------------------- 1 | genre = strtolower($this->genre); 16 | return "This $this->genre title, $this->title, was published by $this->authorName in $this->yearPublished."; 17 | } 18 | 19 | public function setRating(float $ratingOutOf5) 20 | { 21 | $this->bookRating = $ratingOutOf5; 22 | } 23 | 24 | public function getRating() 25 | { 26 | return "GETTER retrieves Rating: $this->bookRating"; 27 | } 28 | 29 | public function getISBN() 30 | { 31 | return $this->isbn; 32 | } 33 | 34 | // runs when object is instantiated 35 | public function __construct($genre, $authorName, $title) 36 | { 37 | $this->genre = $genre; 38 | $this->authorName =$authorName; 39 | $this->title = $title; 40 | 41 | echo "CONSTRUCTOR created " . $this->title . " by " . $this->authorName . " in " . strtolower($this->genre) . " genre.\n"; 42 | } 43 | 44 | // runs when object is destroyed or script is stopped 45 | function __destruct() 46 | { 47 | echo "DESTRUCTOR destroyed object: $this->title."; 48 | } 49 | } 50 | 51 | $book001 = new Book('Non-fiction', 'Bill Bryson', 'A Short History of Nearly Everything'); 52 | 53 | // View output for constructor and destructor 54 | print_r($book001); -------------------------------------------------------------------------------- /05-inheritance.php: -------------------------------------------------------------------------------- 1 | genre = strtolower($this->genre); 16 | return "This $this->genre title, $this->title, was published by $this->authorName in $this->yearPublished."; 17 | } 18 | 19 | public function setRating(float $ratingOutOf5) 20 | { 21 | $this->bookRating = $ratingOutOf5; 22 | } 23 | 24 | public function getRating() 25 | { 26 | return "GETTER retrieves Rating: $this->bookRating"; 27 | } 28 | 29 | public function getISBN() 30 | { 31 | return $this->isbn; 32 | } 33 | 34 | public function __construct($genre, $authorName, $title) 35 | { 36 | $this->genre = $genre; 37 | $this->authorName =$authorName; 38 | $this->title = $title; 39 | 40 | echo "CONSTRUCTOR created " . $this->title . " by " . $this->authorName . " in " . strtolower($this->genre) . " genre.\n"; 41 | } 42 | 43 | function __destruct() 44 | { 45 | echo "DESTRUCTOR destroyed object: $this->title."; 46 | } 47 | } 48 | 49 | // 50 | // INHERITANCE 51 | // 52 | 53 | // extend Book class to Collection class 54 | class Collection extends Book 55 | { 56 | } 57 | 58 | echo "\n"; 59 | $encyclopedia = new Collection('Reference', 'Encyclopedia Britannica', 2014); 60 | print_r($encyclopedia); 61 | 62 | -------------------------------------------------------------------------------- /06-static.php: -------------------------------------------------------------------------------- 1 | genre = strtolower($this->genre); 16 | return "This $this->genre title, $this->title, was published by $this->authorName in $this->yearPublished."; 17 | } 18 | 19 | public function setRating(float $ratingOutOf5) 20 | { 21 | $this->bookRating = $ratingOutOf5; 22 | } 23 | 24 | public function getRating() 25 | { 26 | return "GETTER retrieves Rating: $this->bookRating"; 27 | } 28 | 29 | public function getISBN() 30 | { 31 | return $this->isbn; 32 | } 33 | 34 | public function __construct($genre, $authorName, $title) 35 | { 36 | $this->genre = $genre; 37 | $this->authorName =$authorName; 38 | $this->title = $title; 39 | 40 | echo "CONSTRUCTOR created " . $this->title . " by " . $this->authorName . " in " . strtolower($this->genre) . " genre.\n"; 41 | } 42 | 43 | function __destruct() 44 | { 45 | echo "DESTRUCTOR destroyed object: $this->title."; 46 | } 47 | } 48 | 49 | // 50 | // INHERITANCE 51 | // 52 | 53 | class Collection extends Book 54 | { 55 | // add static property 56 | public static int $noVolumes; 57 | private string $cost; 58 | 59 | // add static function 60 | public static function createCollection() { 61 | // use SELF and :: to refer to static property / function within class 62 | return "Collection has been created with " . SELF::$noVolumes . " volumes.\n"; 63 | } 64 | 65 | public function __construct($genre, $authorName, $title, $isbn, $noVolumes) 66 | { 67 | 68 | parent::__construct($genre, $authorName, $title); 69 | $this->isbn = $isbn; 70 | SELF::$noVolumes = $noVolumes; 71 | 72 | // echo static function on construct for extended child class 73 | echo SELF::createCollection(); 74 | } 75 | 76 | public function setVolumes ($noVolumes) 77 | { 78 | $this->noVolumes = $noVolumes; 79 | } 80 | 81 | public function getVolumes () { 82 | return $this->noVolumes; 83 | } 84 | 85 | public function showCost() 86 | { 87 | return $this->cost; 88 | } 89 | } 90 | 91 | // view instantiation of $encyclopedia object of class collection. 92 | // static properties and methods are called. 93 | echo "\n"; 94 | $encyclopedia = new Collection('Reference', 'multiple authors', 'Encyclopedia Britannica', '9780852293874', 32); 95 | echo "\n"; 96 | echo "Echoing number of volumes from static property: " . Collection::$noVolumes; 97 | echo "\n";echo "\n"; 98 | print_r($encyclopedia); 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /07-override.php: -------------------------------------------------------------------------------- 1 | genre = strtolower($this->genre); 16 | return "This $this->genre title, $this->title, was published by $this->authorName in $this->yearPublished."; 17 | } 18 | 19 | public function setRating(float $ratingOutOf5) 20 | { 21 | $this->bookRating = $ratingOutOf5; 22 | } 23 | 24 | public function getRating() 25 | { 26 | return "GETTER retrieves Rating: $this->bookRating"; 27 | } 28 | 29 | public function getISBN() 30 | { 31 | return $this->isbn; 32 | } 33 | 34 | public function __construct($genre, $authorName, $title) 35 | { 36 | $this->genre = $genre; 37 | $this->authorName =$authorName; 38 | $this->title = $title; 39 | 40 | echo "CONSTRUCTOR created " . $this->title . " by " . $this->authorName . " in " . strtolower($this->genre) . " genre.\n"; 41 | } 42 | 43 | function __destruct() 44 | { 45 | echo "DESTRUCTOR destroyed object: $this->title."; 46 | } 47 | } 48 | 49 | // 50 | // INHERITANCE 51 | // 52 | 53 | class Collection extends Book 54 | { 55 | public static int $noVolumes; 56 | private string $cost; 57 | // override property $noPages, since collections consist of multiple books 58 | public int $noPages = 0; 59 | 60 | public static function createCollection() 61 | { 62 | return "Collection has been created with " . SELF::$noVolumes . " volumes.\n"; 63 | } 64 | 65 | // override getBookInfo, to adapt info given when title is a collection of book volumes 66 | public function getBookInfo () 67 | { 68 | $this->genre = strtolower($this->genre); 69 | return "This $this->genre collection is a " . SELF::$noVolumes . "-volume edition of $this->title."; 70 | } 71 | 72 | public function __construct($genre, $authorName, $title, $isbn, $noVolumes) 73 | { 74 | parent::__construct($genre, $authorName, $title); 75 | $this->isbn = $isbn; 76 | // override original value of $authorName 77 | $this->authorName = 'one or more authors'; 78 | SELF::$noVolumes = $noVolumes; 79 | 80 | // echo static function on construct for extended child class 81 | echo SELF::createCollection(); 82 | } 83 | 84 | public function setVolumes ($noVolumes) 85 | { 86 | $this->noVolumes = $noVolumes; 87 | } 88 | 89 | public function getVolumes () { 90 | return $this->noVolumes; 91 | } 92 | 93 | public function showCost() 94 | { 95 | return $this->cost; 96 | } 97 | } 98 | 99 | // change value of parameter authorName, to see override of property in line 57-58 100 | echo "\n"; 101 | $encyclopedia = new Collection('Reference', 'unknown', 'Encyclopedia Britannica', '9780852293874', 32); 102 | echo "\n"; 103 | // view override of noPages 104 | echo "Echoing noPages to show override: " . $encyclopedia->noPages; 105 | echo "\n"; 106 | echo $encyclopedia->getBookInfo(); 107 | echo "\n"; 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /08-abstract-class.php: -------------------------------------------------------------------------------- 1 | genre = strtolower($this->genre); 26 | return "This $this->genre title, $this->title, was published by $this->authorName in $this->yearPublished."; 27 | } 28 | 29 | public function setRating(float $ratingOutOf5) 30 | { 31 | $this->bookRating = $ratingOutOf5; 32 | } 33 | 34 | public function getRating() 35 | { 36 | return "GETTER retrieves Rating: $this->bookRating"; 37 | } 38 | 39 | public function getISBN() 40 | { 41 | return $this->isbn; 42 | } 43 | 44 | public function __construct($genre, $authorName, $title) 45 | { 46 | $this->genre = $genre; 47 | $this->authorName =$authorName; 48 | $this->title = $title; 49 | 50 | echo "CONSTRUCTOR created " . $this->title . " by " . $this->authorName . " in " . strtolower($this->genre) . " genre.\n"; 51 | } 52 | 53 | function __destruct() 54 | { 55 | echo "DESTRUCTOR destroyed object: $this->title."; 56 | } 57 | } 58 | 59 | class Collection extends Book 60 | { 61 | public static int $noVolumes; 62 | private string $cost; 63 | public int $noPages = 0; 64 | 65 | public static function createCollection() 66 | { 67 | return "Collection has been created with " . SELF::$noVolumes . " volumes.\n"; 68 | } 69 | 70 | public function getBookInfo () 71 | { 72 | $this->genre = strtolower($this->genre); 73 | return "This $this->genre collection is a " . SELF::$noVolumes . "-volume edition of $this->title."; 74 | } 75 | 76 | public function __construct($genre, $authorName, $title, $isbn, $noVolumes) 77 | { 78 | parent::__construct($genre, $authorName, $title); 79 | $this->isbn = $isbn; 80 | // override original value of $authorName 81 | $this->authorName = 'one or more authors'; 82 | SELF::$noVolumes = $noVolumes; 83 | 84 | echo SELF::createCollection(); 85 | } 86 | 87 | public function setVolumes ($noVolumes) 88 | { 89 | $this->noVolumes = $noVolumes; 90 | } 91 | 92 | public function getVolumes () { 93 | return $this->noVolumes; 94 | } 95 | 96 | public function showCost() 97 | { 98 | return $this->cost; 99 | } 100 | } 101 | 102 | // try to instantiate $book from Books class, but can't because it's abstract 103 | // $book= new Books(); 104 | // echo $book; // Fatal error: Uncaught Error: Cannot instantiate abstract class Books 105 | 106 | 107 | // instantiation still works for child class, Book 108 | $book001 = new Book('Non-fiction', 'Bill Bryson', 'A Short History of Nearly Everything'); 109 | 110 | // View output for constructor and destructor 111 | print_r($book001); 112 | 113 | -------------------------------------------------------------------------------- /09-interface.php: -------------------------------------------------------------------------------- 1 | conn = true; 27 | return 'Opening connection to ' . LIBRARY_SYSTEM; 28 | } 29 | 30 | // implemented method 31 | public function verifyService() 32 | { 33 | if ($this->conn == true) { 34 | return 'Service verified'; 35 | } else { 36 | return 'Unable to locate service'; 37 | } 38 | } 39 | // implemented method 40 | public function login($username, $password) 41 | { 42 | $this->username = $username; 43 | $this->password = $password; 44 | return "You are now logged in as $this->username."; 45 | } 46 | // implemented method 47 | public function logout() 48 | { 49 | $this->username = null; 50 | $this->password = null; 51 | return "You are now logged out."; 52 | } 53 | 54 | public static function searchLibrary(string $isbn) 55 | { 56 | return "Searching local library service for $isbn."; 57 | } 58 | 59 | public function __construct() 60 | { 61 | // construct to call openConnection() from inside class; 62 | echo self::openConnection(); 63 | } 64 | } 65 | 66 | 67 | 68 | abstract class Books 69 | { 70 | public string $title; 71 | public string $authorName; 72 | public string $genre; 73 | abstract public function getBookInfo(); 74 | abstract protected function getISBN(); 75 | } 76 | 77 | class Book extends Books 78 | { 79 | 80 | public int $yearPublished; 81 | public int $noPages; 82 | private float $bookRating; 83 | protected string $isbn; 84 | 85 | public function getBookInfo () 86 | { 87 | $this->genre = strtolower($this->genre); 88 | return "This $this->genre title, $this->title, was published by $this->authorName in $this->yearPublished."; 89 | } 90 | 91 | public function setRating(float $ratingOutOf5) 92 | { 93 | $this->bookRating = $ratingOutOf5; 94 | } 95 | 96 | public function getRating() 97 | { 98 | return "GETTER retrieves Rating: $this->bookRating"; 99 | } 100 | 101 | // add setter to make sure isbn is available to library service 102 | public function setISBN(string $isbn) 103 | { 104 | $this->isbn = $isbn; 105 | } 106 | 107 | public function getISBN() 108 | { 109 | return $this->isbn; 110 | } 111 | 112 | public function __construct($genre, $authorName, $title) 113 | { 114 | $this->genre = $genre; 115 | $this->authorName =$authorName; 116 | $this->title = $title; 117 | 118 | echo "CONSTRUCTOR created " . $this->title . " by " . $this->authorName . " in " . strtolower($this->genre) . " genre.\n"; 119 | } 120 | 121 | function __destruct() 122 | { 123 | echo "DESTRUCTOR destroyed object: $this->title."; 124 | } 125 | } 126 | 127 | class Collection extends Book 128 | { 129 | public static int $noVolumes; 130 | private string $cost; 131 | public int $noPages = 0; 132 | 133 | public static function createCollection() 134 | { 135 | return "Collection has been created with " . SELF::$noVolumes . " volumes.\n"; 136 | } 137 | 138 | public function getBookInfo () 139 | { 140 | $this->genre = strtolower($this->genre); 141 | return "This $this->genre collection is a " . SELF::$noVolumes . "-volume edition of $this->title."; 142 | } 143 | 144 | public function __construct($genre, $authorName, $title, $isbn, $noVolumes) 145 | { 146 | parent::__construct($genre, $authorName, $title); 147 | $this->isbn = $isbn; 148 | // override original value of $authorName 149 | $this->authorName = 'one or more authors'; 150 | SELF::$noVolumes = $noVolumes; 151 | 152 | echo SELF::createCollection(); 153 | } 154 | 155 | public function setVolumes ($noVolumes) 156 | { 157 | $this->noVolumes = $noVolumes; 158 | } 159 | 160 | public function getVolumes () { 161 | return $this->noVolumes; 162 | } 163 | 164 | public function showCost() 165 | { 166 | return $this->cost; 167 | } 168 | } 169 | 170 | 171 | // INSTANTIATE book obj 172 | $book001 = new Book('Non-fiction', 'Bill Bryson', 'A Short History of Nearly Everything'); 173 | 174 | // set isbn to access for library service search 175 | $book001->setISBN('076790818X'); 176 | print_r($book001); 177 | echo $bookISBN = $book001->getISBN(); 178 | 179 | echo "\n"; 180 | // not in construct, openConnection is called, making connection ready for verifyService() 181 | $newConnection = new Connection(); 182 | echo "\n"; 183 | 184 | // verify connection to service 185 | echo $newConnection->verifyService(); 186 | echo "\n"; 187 | 188 | // login 189 | echo $newConnection->login('user', 'user12345'); 190 | echo "\n"; 191 | 192 | // search for book isbn 193 | echo $newConnection->searchLibrary($bookISBN); 194 | echo "\n"; 195 | 196 | // log out 197 | echo $newConnection->logout(); 198 | echo "\n"; 199 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # oop-basics-practical 2 | 3 | 4 | # Assignment / Pill: Assembler Institute of Technology -- OOP Basics with PHP 5 | 6 | In this assignment, all files are examples provided to practice differenct basic concepts in OOP. A library service and books were used as a reference or practical exaqmple throughout the assignment. You can find a description of each file's content in _"Project files"_. 7 | 8 | ## Table of contents 9 | 10 | - [Table of contents](#table-of-contents) 11 | - [Getting Started](#Getting-Started) 12 | - [Introduction](#OOP-Basics-Practical) 13 | 14 | - [Project files](#project-files) 15 | 16 | ## Getting Started 17 | 18 | ### The repo 19 | 20 | Assembler repo used as guide, instructions, and to outline file structure. 21 | 22 | ```bash 23 | https://github.com/assembler-school/oop-basics.git 24 | ``` 25 | 26 | 27 | ## OOP Basics Practical 28 | 29 | Task is to create an organized file structure to complete the pill assignment. 30 | 31 | ## Project files 32 | 33 | ### [01 - Classes](./01-base-class.php) 34 | 35 | Add base-class Book 36 | 37 | ### [02 - Properties](02-properties.php) 38 | 39 | Add properties to base class Book and instantiate object to view object. 40 | 41 | ### [03 - Methods](03-methods.php) 42 | 43 | Add methods to Book class, including getters and setters. 44 | 45 | ### [04 - Constructors](04-constructors.php) 46 | 47 | Constructor and Destructor functions added to Book class. 48 | 49 | ### [05 - Inheritance](05-inheritance.php) 50 | 51 | Basic 'Collection' child class created from Book parent class. 52 | 53 | ### [06 - Static](06-static.php) 54 | 55 | 'Collection' child class expanded with properties, functions, getters, setters, as well as static properties and functions. 56 | 57 | ### [07 - Override](07-override.php) 58 | 59 | Override is used to change behavior of property and function, within class Collection. 60 | 61 | ### [08 - Abstract Classes](08-abstract-class.php) 62 | 63 | Abstract class Books is created. Book class is extended from Books and properties and functions are moved or created inside Books. 64 | 65 | ### [09 - Interfaces](09-interface.php) 66 | 67 | Interface libraryService is added as an example. |Class Connection is added to implement libraryService and add functionality to implemented functions. 68 | --------------------------------------------------------------------------------