Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: DubStepMad
- * Date: 12/10/2018
- * Time: 3:26 PM
- */
- /** This is the superclass **/
- class fruit
- {
- private static $instanceFruit;
- public static function createObject($_weight, $_price, $_quantity){
- if(!isset(self::$instanceFruit)){
- self::$instanceFruit = new self($_weight, $_price, $_quantity);
- }else{
- echo "Cannot create a new Fruit, there can only be one fruit <br>";
- }
- return self::$instanceFruit;
- }
- /** Fruit private variables **/
- /** Using Grams **/
- private $_weight;
- /** Using GBP **/
- private $_price;
- private $_quantity;
- public function __construct($_weight, $_price, $_quantity) {
- echo "in constructor for ".__CLASS__."<br>";
- $this->_weight = $_weight;
- $this->_price = $_price;
- $this->_quantity = $_quantity;
- }
- public function __destruct(){
- echo 'The class "',__CLASS__, '" was destroyed.<br />';
- }
- public function __toString()
- {
- $properties = __CLASS__." properties : "
- ."Weight: ".$this->_weight."g, "
- ."Price: £".$this->_price.", "
- ."Quantity: ".$this->_quantity."<br>";
- return $properties;
- }
- /** Get and Set for Fruit Class variables **/
- /**
- * @param mixed $weight
- */
- public function setWeight($weight)
- {
- $this->_weight = $weight;
- }
- /**
- * @return mixed
- */
- public function getWeight()
- {
- return $this->_weight;
- }
- /**
- * @param mixed $price
- */
- public function setPrice($price)
- { if($price < 300){
- $this->_price = "Invaild Price";
- }else {
- $this->_price = $price;
- }
- }
- /**
- * @return mixed
- */
- public function getPrice()
- {
- return $this->_price;
- }
- /**
- * @param mixed $quantity
- */
- public function setQuantity($quantity)
- {
- $this->_quantity = $quantity;
- }
- /**
- * @return mixed
- */
- public function getQuantity()
- {
- return $this->_quantity;
- }
- }
- class Apple extends fruit {
- /** Apple private variables **/
- private $_name;
- private $_store;
- private $_farmLocation;
- public function __construct($_weight, $_price, $_quantity,$_name,$_store,$_farmLocation){
- echo "in constructor for ".__CLASS__."<br>";
- parent::__construct($_weight, $_price, $_quantity);
- $this->_name = $_name;
- $this->_store = $_store;
- $this->_farmLocation = $_farmLocation;
- }
- public function __destruct(){
- echo 'The class "',__CLASS__, '" was destroyed.<br />';
- }
- public function __toString()
- {
- /** Calls parent variables **/
- $properties = parent::__toString();
- $properties = $properties.__CLASS__." properties : "
- ."Product Name: ".$this->_name.", "
- ."Store: ".$this->_store.", "
- ."Farm Location: ".$this->_farmLocation."<br>";
- return $properties;
- }
- /** Get and Set for Apple Class variables **/
- /**
- * @param mixed $name
- */
- public function setName($name)
- {
- $this->_name = $name;
- }
- /**
- * @return mixed
- */
- public function getName()
- {
- return $this->_name;
- }
- /**
- * @param mixed $store
- */
- public function setStore($store)
- {
- $this->_name = $store;
- }
- /**
- * @return mixed
- */
- public function getStore()
- {
- return $this->_store;
- }
- /**
- * @param mixed $farmLocation
- */
- public function setFarmLocation($farmLocation)
- {
- $this->_farmLocation = $farmLocation;
- }
- /**
- * @return mixed
- */
- public function getFarmLocation()
- {
- return $this->_farmLocation;
- }
- }
- class Orange extends fruit {
- /** Orange private variables **/
- private $_transportType;
- /** Using Years **/
- private $_age;
- public function __construct($_weight, $_price, $_quantity, $_transportType, $_age){
- echo "in constructor for ".__CLASS__."<br>";
- parent::__construct($_weight, $_price, $_quantity);
- $this->_transportType = $_transportType;
- $this->_age = $_age;
- }
- public function __destruct(){
- echo 'The class "',__CLASS__, '" was destroyed.<br />';
- }
- public function __toString()
- {
- /** Calls parent variables **/
- $properties = parent::__toString();
- $properties = $properties.__CLASS__." properties : "
- ."Transport Type: ".$this->_transportType.", "
- ."Aged: ".$this->_age." Years old<br>";
- return $properties;
- }
- /** Get and Set for Orange Class variables **/
- /**
- * @param mixed $transportType
- */
- public function setTransportType($transportType)
- {
- $this->_transportType = $transportType;
- }
- /**
- * @return mixed
- */
- public function getTransportType()
- {
- return $this->_transportType;
- }
- /**
- * @param mixed $age
- */
- public function setAge($age)
- { if($age < 2){
- $this->_age = "Invaild Age";
- }else {
- $this->_age = $age;
- }
- }
- /**
- * @return mixed
- */
- public function getAge()
- {
- return $this->_age;
- }
- }
- class Pineapple extends fruit {
- /** Pineapple private variables **/
- private $_quality;
- /** Using Years **/
- private $_country;
- public function __construct($_weight, $_price, $_quantity, $_quality, $_country){
- echo "in constructor for ".__CLASS__."<br>";
- parent::__construct($_weight, $_price, $_quantity);
- $this->_quality = $_quality;
- $this->_country = $_country;
- }
- public function __destruct(){
- echo 'The class "',__CLASS__, '" was destroyed.<br />';
- }
- public function __toString()
- {
- /** Calls parent variables **/
- $properties = parent::__toString();
- $properties = $properties.__CLASS__." properties : "
- ."Quality Grade: ".$this->_quality.", "
- ."Originated Country: ".$this->_country."<br>";
- return $properties;
- }
- /** Get and Set for Pineapple Class variables **/
- /**
- * @param mixed $quality
- */
- public function setQuality($quality)
- { if($quality < 0){
- $this->_quality = "Invaild Quality Grade";
- }else {
- $this->_quality = $quality;
- }
- }
- /**
- * @return mixed
- */
- public function getQuality()
- {
- return $this->_quality;
- }
- /**
- * @param mixed $country
- */
- public function setCountry($country)
- {
- $this->_country = $country;
- }
- /**
- * @return mixed
- */
- public function getCountry()
- {
- return $this->_country;
- }
- }
- echo "Start of Script<br>";
- $order1 = new Orange(2,"100",50, "Ship", "5");
- echo $order1;
- $order2 = new Apple(500,"150",50, "RedLady", "TESCO", "Lakeside Farm");
- echo $order2;
- $order3 = new Fruit(25,"50",15);
- echo $order3;
- $order4 = new Pineapple(700,"450",100, "A*", "Brazil");
- echo $order4;
- $order5 = Fruit::createObject(25,"50",15);
- echo $order5;
- $order6 = Fruit::createObject(25,"50",15);
- echo $order6;
- echo "End of Script <br>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement