Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: DubStepMad
  5. * Date: 12/10/2018
  6. * Time: 3:26 PM
  7. */
  8.  
  9. /** This is the superclass **/
  10. class fruit
  11. {
  12.  
  13. private static $instanceFruit;
  14.  
  15. public static function createObject($_weight, $_price, $_quantity){
  16. if(!isset(self::$instanceFruit)){
  17. self::$instanceFruit = new self($_weight, $_price, $_quantity);
  18. }else{
  19. echo "Cannot create a new Fruit, there can only be one fruit <br>";
  20. }
  21. return self::$instanceFruit;
  22. }
  23.  
  24. /** Fruit private variables **/
  25. /** Using Grams **/
  26. private $_weight;
  27. /** Using GBP **/
  28. private $_price;
  29. private $_quantity;
  30.  
  31. public function __construct($_weight, $_price, $_quantity) {
  32.  
  33. echo "in constructor for ".__CLASS__."<br>";
  34.  
  35. $this->_weight = $_weight;
  36. $this->_price = $_price;
  37. $this->_quantity = $_quantity;
  38. }
  39.  
  40. public function __destruct(){
  41. echo 'The class "',__CLASS__, '" was destroyed.<br />';
  42. }
  43.  
  44. public function __toString()
  45. {
  46. $properties = __CLASS__." properties : "
  47. ."Weight: ".$this->_weight."g, "
  48. ."Price: £".$this->_price.", "
  49. ."Quantity: ".$this->_quantity."<br>";
  50. return $properties;
  51. }
  52.  
  53. /** Get and Set for Fruit Class variables **/
  54.  
  55. /**
  56. * @param mixed $weight
  57. */
  58. public function setWeight($weight)
  59. {
  60. $this->_weight = $weight;
  61. }
  62.  
  63. /**
  64. * @return mixed
  65. */
  66. public function getWeight()
  67. {
  68. return $this->_weight;
  69. }
  70.  
  71. /**
  72. * @param mixed $price
  73. */
  74. public function setPrice($price)
  75. { if($price < 300){
  76. $this->_price = "Invaild Price";
  77. }else {
  78. $this->_price = $price;
  79. }
  80. }
  81.  
  82. /**
  83. * @return mixed
  84. */
  85. public function getPrice()
  86. {
  87. return $this->_price;
  88. }
  89.  
  90. /**
  91. * @param mixed $quantity
  92. */
  93. public function setQuantity($quantity)
  94. {
  95. $this->_quantity = $quantity;
  96. }
  97.  
  98. /**
  99. * @return mixed
  100. */
  101. public function getQuantity()
  102. {
  103. return $this->_quantity;
  104. }
  105. }
  106.  
  107. class Apple extends fruit {
  108.  
  109. /** Apple private variables **/
  110. private $_name;
  111. private $_store;
  112. private $_farmLocation;
  113.  
  114. public function __construct($_weight, $_price, $_quantity,$_name,$_store,$_farmLocation){
  115.  
  116. echo "in constructor for ".__CLASS__."<br>";
  117.  
  118. parent::__construct($_weight, $_price, $_quantity);
  119.  
  120. $this->_name = $_name;
  121. $this->_store = $_store;
  122. $this->_farmLocation = $_farmLocation;
  123. }
  124.  
  125. public function __destruct(){
  126. echo 'The class "',__CLASS__, '" was destroyed.<br />';
  127. }
  128.  
  129. public function __toString()
  130. {
  131. /** Calls parent variables **/
  132. $properties = parent::__toString();
  133. $properties = $properties.__CLASS__." properties : "
  134. ."Product Name: ".$this->_name.", "
  135. ."Store: ".$this->_store.", "
  136. ."Farm Location: ".$this->_farmLocation."<br>";
  137. return $properties;
  138. }
  139.  
  140. /** Get and Set for Apple Class variables **/
  141.  
  142. /**
  143. * @param mixed $name
  144. */
  145. public function setName($name)
  146. {
  147. $this->_name = $name;
  148. }
  149.  
  150. /**
  151. * @return mixed
  152. */
  153. public function getName()
  154. {
  155. return $this->_name;
  156. }
  157.  
  158. /**
  159. * @param mixed $store
  160. */
  161. public function setStore($store)
  162. {
  163. $this->_name = $store;
  164. }
  165.  
  166. /**
  167. * @return mixed
  168. */
  169. public function getStore()
  170. {
  171. return $this->_store;
  172. }
  173.  
  174. /**
  175. * @param mixed $farmLocation
  176. */
  177. public function setFarmLocation($farmLocation)
  178. {
  179. $this->_farmLocation = $farmLocation;
  180. }
  181.  
  182. /**
  183. * @return mixed
  184. */
  185. public function getFarmLocation()
  186. {
  187. return $this->_farmLocation;
  188. }
  189. }
  190.  
  191. class Orange extends fruit {
  192.  
  193. /** Orange private variables **/
  194. private $_transportType;
  195. /** Using Years **/
  196. private $_age;
  197.  
  198. public function __construct($_weight, $_price, $_quantity, $_transportType, $_age){
  199.  
  200. echo "in constructor for ".__CLASS__."<br>";
  201.  
  202. parent::__construct($_weight, $_price, $_quantity);
  203.  
  204. $this->_transportType = $_transportType;
  205. $this->_age = $_age;
  206.  
  207. }
  208.  
  209. public function __destruct(){
  210. echo 'The class "',__CLASS__, '" was destroyed.<br />';
  211. }
  212.  
  213. public function __toString()
  214. {
  215. /** Calls parent variables **/
  216. $properties = parent::__toString();
  217. $properties = $properties.__CLASS__." properties : "
  218. ."Transport Type: ".$this->_transportType.", "
  219. ."Aged: ".$this->_age." Years old<br>";
  220. return $properties;
  221. }
  222.  
  223. /** Get and Set for Orange Class variables **/
  224.  
  225. /**
  226. * @param mixed $transportType
  227. */
  228. public function setTransportType($transportType)
  229. {
  230. $this->_transportType = $transportType;
  231. }
  232.  
  233. /**
  234. * @return mixed
  235. */
  236. public function getTransportType()
  237. {
  238. return $this->_transportType;
  239. }
  240.  
  241. /**
  242. * @param mixed $age
  243. */
  244. public function setAge($age)
  245. { if($age < 2){
  246. $this->_age = "Invaild Age";
  247. }else {
  248. $this->_age = $age;
  249. }
  250. }
  251.  
  252. /**
  253. * @return mixed
  254. */
  255. public function getAge()
  256. {
  257. return $this->_age;
  258. }
  259. }
  260.  
  261. class Pineapple extends fruit {
  262.  
  263. /** Pineapple private variables **/
  264. private $_quality;
  265. /** Using Years **/
  266. private $_country;
  267.  
  268. public function __construct($_weight, $_price, $_quantity, $_quality, $_country){
  269.  
  270. echo "in constructor for ".__CLASS__."<br>";
  271.  
  272. parent::__construct($_weight, $_price, $_quantity);
  273.  
  274. $this->_quality = $_quality;
  275. $this->_country = $_country;
  276.  
  277. }
  278.  
  279. public function __destruct(){
  280. echo 'The class "',__CLASS__, '" was destroyed.<br />';
  281. }
  282.  
  283. public function __toString()
  284. {
  285. /** Calls parent variables **/
  286. $properties = parent::__toString();
  287. $properties = $properties.__CLASS__." properties : "
  288. ."Quality Grade: ".$this->_quality.", "
  289. ."Originated Country: ".$this->_country."<br>";
  290. return $properties;
  291. }
  292.  
  293. /** Get and Set for Pineapple Class variables **/
  294.  
  295. /**
  296. * @param mixed $quality
  297. */
  298. public function setQuality($quality)
  299. { if($quality < 0){
  300. $this->_quality = "Invaild Quality Grade";
  301. }else {
  302. $this->_quality = $quality;
  303. }
  304. }
  305.  
  306. /**
  307. * @return mixed
  308. */
  309. public function getQuality()
  310. {
  311. return $this->_quality;
  312. }
  313.  
  314. /**
  315. * @param mixed $country
  316. */
  317. public function setCountry($country)
  318. {
  319. $this->_country = $country;
  320. }
  321.  
  322. /**
  323. * @return mixed
  324. */
  325. public function getCountry()
  326. {
  327. return $this->_country;
  328. }
  329. }
  330.  
  331. echo "Start of Script<br>";
  332.  
  333. $order1 = new Orange(2,"100",50, "Ship", "5");
  334.  
  335. echo $order1;
  336.  
  337. $order2 = new Apple(500,"150",50, "RedLady", "TESCO", "Lakeside Farm");
  338.  
  339. echo $order2;
  340.  
  341. $order3 = new Fruit(25,"50",15);
  342.  
  343. echo $order3;
  344.  
  345. $order4 = new Pineapple(700,"450",100, "A*", "Brazil");
  346.  
  347. echo $order4;
  348.  
  349. $order5 = Fruit::createObject(25,"50",15);
  350.  
  351. echo $order5;
  352.  
  353. $order6 = Fruit::createObject(25,"50",15);
  354.  
  355. echo $order6;
  356.  
  357. echo "End of Script <br>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement