Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. <?php
  2.  
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5. error_reporting(E_ALL);
  6.  
  7. class Father {
  8.  
  9. // Property
  10. public $lastName;
  11.  
  12. // Method
  13. public function getLastName() {
  14. return $this->lastName;
  15. }
  16.  
  17. }
  18.  
  19. //////////////////////////////////////
  20.  
  21. class Father {
  22.  
  23. }
  24.  
  25. class Son extends Father {
  26.  
  27. }
  28.  
  29. ////////////////////////////////////////
  30.  
  31. class Father {
  32.  
  33. protected $lastName = 'OOP';
  34.  
  35. public function getLastName() {
  36. return $this->lastName;
  37. }
  38.  
  39. }
  40.  
  41. class Son extends Father {
  42.  
  43. public function getLastName() {
  44. return 'Not ' . $this->lastName;
  45. }
  46.  
  47. }
  48.  
  49. $father = new Father();
  50. echo $father->getLastName(); // return OOP
  51.  
  52. $son = new Son();
  53. echo $son->getLastName(); // return Not OOP
  54.  
  55. ////////////////////////////////////////
  56.  
  57. abstract class Person {
  58.  
  59. }
  60.  
  61. class Father extends Person {
  62.  
  63. }
  64.  
  65. final class Son extends Father {
  66.  
  67. }
  68.  
  69. class Grandson extends Son {
  70.  
  71. }
  72.  
  73. $person = new Person(); // return Cannot instantiate class Person
  74. $father = new Father();
  75. $son = new Son();
  76. $grandson = new Grandson(); // Cannot exist. To declare an extension
  77. // of a final class kills the execution
  78.  
  79. ////////////////////////////////////////
  80.  
  81. class Father {
  82.  
  83. public $public;
  84. protected $protected;
  85. private $private;
  86.  
  87. }
  88.  
  89. ////////////////////////////////////////
  90.  
  91. class Numbers {
  92.  
  93. const pi = 3.14;
  94.  
  95. public function getPi() {
  96. return self::pi;
  97. }
  98.  
  99. }
  100.  
  101. $numbers = new Numbers();
  102. echo $numbers->getPi(); // return 3.14
  103.  
  104. ////////////////////////////////////////
  105.  
  106. class Numbers {
  107.  
  108. const pi = 3.14;
  109.  
  110. public function getPi() {
  111. return self::pi;
  112. }
  113.  
  114. }
  115.  
  116. class App extends Numbers {
  117.  
  118. const version = 1.0;
  119.  
  120. public function getItAll() {
  121. echo 'App version: ' . self::version;
  122. echo 'Pi number: ' . parent::pi;
  123. }
  124.  
  125. }
  126.  
  127. $app = new App();
  128. echo $app->getPi(); // return 3.14
  129. echo $app->getItAll(); // return App version: 1.0 Pi number: 3.14
  130.  
  131. ////////////////////////////////////////
  132.  
  133. class Statics {
  134.  
  135. public const staticConst = 'Static Const';
  136.  
  137. public static $staticProperty = 'Static Property';
  138.  
  139. public static function staticMethod() {
  140. return 'Static Method';
  141. }
  142.  
  143. }
  144.  
  145. echo Statics::staticConst; // return Static Const
  146. echo Statics::$staticProperty; // return Static Property
  147. echo Statics::staticMethod(); // return Static Method
  148.  
  149. ////////////////////////////////////////
  150.  
  151. class WineHouse {
  152.  
  153. public $name = 'Wine House';
  154.  
  155. }
  156.  
  157. class Wine {
  158.  
  159. public $wineHouse;
  160.  
  161. public function __construct($wineHouse) {
  162. $this->wineHouse = $wineHouse;
  163. }
  164.  
  165. }
  166.  
  167. $wineHouse = new WineHouse();
  168. $wine = new Wine($wineHouse);
  169.  
  170. echo $wine->wineHouse->name; // return Wine House
  171.  
  172. //////////////////////////////////////
  173.  
  174. class Product {
  175.  
  176. public $name;
  177.  
  178. public function __construct($name) {
  179. $this->name = $name;
  180. }
  181.  
  182. }
  183.  
  184. class Cart {
  185.  
  186. public $products;
  187.  
  188. public function addProduct(Product $product) {
  189. $this->products[] = $product;
  190. }
  191.  
  192. public function getAllProducts() {
  193. return $this->products;
  194. }
  195.  
  196. }
  197.  
  198. $productA = new Product('Product A');
  199. $productB = new Product('Product B');
  200. $productC = new Product('Product C');
  201.  
  202. $cart = new Cart();
  203. $cart->addProduct($productA);
  204. $cart->addProduct($productB);
  205. $cart->addProduct($productC);
  206.  
  207. print_r($cart->getAllProducts());
  208. /*
  209.  
  210. return Array (
  211. [0] => Product Object ( [name] => Product A )
  212. [1] => Product Object ( [name] => Product B )
  213. [2] => Product Object ( [name] => Product C )
  214. )
  215.  
  216. */
  217.  
  218. //////////////////////////////////////
  219.  
  220. class Contact {
  221.  
  222. public $name = 'Contact';
  223.  
  224. }
  225.  
  226. class Client {
  227.  
  228. public $contact;
  229.  
  230. public function __construct() {
  231.  
  232. $this->contact = new Contact();
  233.  
  234. }
  235.  
  236. }
  237.  
  238. $client = new Client();
  239. echo $client->contact->name;
  240.  
  241. ////////////////////////////////////////
  242.  
  243. class Person {
  244.  
  245. private $age;
  246.  
  247. public function __set($property, $value) {
  248. $this->property = $value;
  249. echo $property . ' = ' . $value;
  250. }
  251.  
  252. public function __get($property) {
  253. echo 'This property is private/protected: ' . $property;
  254. }
  255.  
  256. public function __call($method, $parameters) {
  257. echo 'The method '. $method . ' does not exist.';
  258. }
  259.  
  260. }
  261.  
  262. $person = new Person();
  263. $person->age = 30; // return age = 30
  264. echo $person->age; // return This property is private/protected: age
  265.  
  266. $person->setFavoriteMovie('Lion King'); // return The method setFavoriteMovie does not exist
  267.  
  268. //////////////////////////////////////
  269.  
  270. require_once('ClassA.php');
  271. require_once('ClassB.php');
  272. require_once('ClassC.php');
  273.  
  274. function __autoload($class) {
  275.  
  276. require_once($class . '.php');
  277.  
  278. }
  279.  
  280. $objectA = new ClassA();
  281. $objectB = new ClassB();
  282. $objectC = new ClassC();
  283.  
  284. echo $objectA->name;
  285. echo $objectB->name;
  286. echo $objectC->name;
  287.  
  288. //////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement