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>

Link to comment
Share on other sites

 <!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

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.