Jump to content

Just A quick Object oriented help please


harrywolves

Recommended Posts

Hi, i am trying to add another propertie to the CDproduct class but everytime i keep coming up with an error:

 

can anybody help please, here is the code below, i know it is something simple but i cannot figure it out, thanks in advance:

 

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  </head>
  <body>

<?php

class ShopProduct
      { 
        public $title;
        public $producerMainName;
        public $producerFirstName;
        public $price;

              function __construct($title, $firstName, $mainName, $price)
                {
                  $this->title = $title;
                  $this->producerFirstName = $firstName;
                  $this->producerMainName = $mainName;
                  $this->price = $price;
                }
          
                      public function getProducer()
                      {
                        return "$this->producerFirstName 
                                $this->producerMainName";
                      }
          
                              public function getProductDetails()
                              {
                                $s = "$this->title ($this->producerFirstName $this->producerMainName)";
                                return $s;
                              }
      
      }
        
    class CdProduct extends ShopProduct
          {
              public $playLength;
                
                // Let's override the constructor
                function __construct($title, $firstName, $mainName, $price, $playLength)
                {
                  parent::__construct($title, $firstName, $mainName, $price);
                  $this->playLength = $playLength;
                }

                         function getPlayLength()
                          {
                            return $this->playLength;
                          }  
                          
                                // Let's override getProductDetails
                                function getProductDetails()
                                {
                                  $s = parent::getProductDetails();
                                  $s.= " - playing time : $this->playLength";
                                  return $s;
                                }
              
                public $starsign;
                            
                            // Let's override the constructor
                            function __construct($title, $firstName, $mainName, $price, $starsign)
                            {
                            parent::__construct($title, $firstName, $mainName, $price);
                            $this->starsign = $starsign;
                            }
                            
                            function getstarsign()
                            {
                            return $this->starsign;
                            }
                            
                            // Let's override getProductDetails
                            function getProductDetails()
                            {
                            $s = parent::getProductDetails();
                            $s.= " - zodiac sign : $this->starsign";
                            return $s;
                            }
                      }


                 
          
     class BookProduct extends ShopProduct
        {
              public $numPages;
                
                // Let's override the constructor
                function __construct($title, $firstName, $mainName, $price, $numPages)
                {
                  parent::__construct($title, $firstName, $mainName, $price);
                  $this->numPages = $numPages;
                } 

                     function getNumberOfPages()
                      {
                        return $this->numPages;
                      }  
  
                      // Let's override getProductDetails
                      function getProductDetails()
                      {
                        $s = parent::getProductDetails();
                        $s.= " - number of pages : $this->numPages";
                        return $s;
                      }  
      }
           
           
           
            $CD1 = new CdProduct("Back to black", "Amy", "Winehouse", 7.99, 63, leo);
            $CD2 = new CdProduct("Back to bedlam", "James", "Blunt", 7.99, 51, leo);

            $Book1 = new BookProduct("1984", "George", "Orwell", 8.99, 352);
            $Book2 = new BookProduct("Never Let", "Kazuo", "Ishiguro", 7.99, 276);
          
            echo $CD1->getProductDetails()."<br>";
            echo $CD2->getProductDetails()."<br>";
            echo $Book1->getProductDetails()."<br>";
            echo $Book2->getProductDetails()."<br>";

?>

</body>
</html>

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  </head>
  <body>

<?php

class ShopProduct
      { 
        public $title;
        public $producerMainName;
        public $producerFirstName;
        public $price;

              function __construct($title, $firstName, $mainName, $price)
                {
                  $this->title = $title;
                  $this->producerFirstName = $firstName;
                  $this->producerMainName = $mainName;
                  $this->price = $price;
                }
          
                      public function getProducer()
                      {
                        return "$this->producerFirstName 
                                $this->producerMainName";
                      }
          
                              public function getProductDetails()
                              {
                                $s = "$this->title ($this->producerFirstName $this->producerMainName)";
                                return $s;
                              }
      
      }
        
    class CdProduct extends ShopProduct
          {
              public $playLength;
                
                // Let's override the constructor
                function __construct($title, $firstName, $mainName, $price, $playLength)
                {
                  parent::__construct($title, $firstName, $mainName, $price);
                  $this->playLength = $playLength;
                }

                         function getPlayLength()
                          {
                            return $this->playLength;
                          }  
                          
                                // Let's override getProductDetails
                                function getProductDetails()
                                {
                                  $s = parent::getProductDetails();
                                  $s.= " - playing time : $this->playLength";
                                  return $s;
                                }
              
                public $starsign;
                            
                            // Let's override the constructor
                            function __construct($title, $firstName, $mainName, $price, $starsign)
                            {
                            parent::__construct($title, $firstName, $mainName, $price);
                            $this->starsign = $starsign;
                            }
                            
                            function getstarsign()
                            {
                            return $this->starsign;
                            }
                            
                            // Let's override getProductDetails
                            function getProductDetails()
                            {
                            $s = parent::getProductDetails();
                            $s.= " - zodiac sign : $this->starsign";
                            return $s;
                            }
                      }


                 
          
     class BookProduct extends ShopProduct
        {
              public $numPages;
                
                // Let's override the constructor
                function __construct($title, $firstName, $mainName, $price, $numPages)
                {
                  parent::__construct($title, $firstName, $mainName, $price);
                  $this->numPages = $numPages;
                } 

                     function getNumberOfPages()
                      {
                        return $this->numPages;
                      }  
  
                      // Let's override getProductDetails
                      function getProductDetails()
                      {
                        $s = parent::getProductDetails();
                        $s.= " - number of pages : $this->numPages";
                        return $s;
                      }  
      }
           
           
           
            $CD1 = new CdProduct("Back to black", "Amy", "Winehouse", 7.99, 63, leo);
            $CD2 = new CdProduct("Back to bedlam", "James", "Blunt", 7.99, 51, leo);

            $Book1 = new BookProduct("1984", "George", "Orwell", 8.99, 352);
            $Book2 = new BookProduct("Never Let", "Kazuo", "Ishiguro", 7.99, 276);
          
            echo $CD1->getProductDetails()."<br>";
            echo $CD2->getProductDetails()."<br>";
            echo $Book1->getProductDetails()."<br>";
            echo $Book2->getProductDetails()."<br>";

?>

</body>
</html>

 

 

Fatal error: Cannot redeclare CdProduct::__construct() in /studhome/0/1018232/public_html/classes/shopproduct.php on line 68

 

and that is the error i recieve shown above, can you tell me what the problem is please thank you

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.