Jump to content

Fatal error: Cannot redeclare class


Sanjib Sinha

Recommended Posts

I wanted to inherit child classes from parent class. But it gives error like this:

Fatal error: Cannot redeclare class Ch2_Product in C:\wamp\www\OOPhpSolutions\Ch_02\Product.php on line 3

 

I have file like this:

Product.php

<?php

class Ch2_Product

{

protected $_title;

protected $_type;

 

public function __construct($title, $type)

{

$this->_title = $title;

$this->_type = $type;

}

 

public function getTitle()

{

return $this->_title;

}

public function getType()

{

return $this->_type;

}

}

?>

 

Next I've inherited like this:

Book.php

<?php

require 'Product.php';

class Ch2_Book extends Ch2_Product

{

//defining functions

}

 

?>

And DVD.php

<?php

require 'Product.php';

 

class Ch2_DVD extends Ch2_Product

{

//

}

?>

Finally I tried to catch the child class values like this in Ch_Book.php

<?php

require_once 'Book.php';

require_once 'DVD.php';

 

$product1 = new Ch2_Book('Old Man and the Sea', 'Novel');

$product2 = new Ch2_DVD('Gone With the Wind', 'Movie');

echo $product1->getTitle();

echo $product1->getType();

 

echo $product2->getTitle();

echo $product2->getType();

 

?>

 

But it gave that error. Can anyone solve this?

 

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.