Sanjib Sinha Posted May 12, 2011 Share Posted May 12, 2011 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 https://forums.phpfreaks.com/topic/236204-fatal-error-cannot-redeclare-class/ Share on other sites More sharing options...
trq Posted May 12, 2011 Share Posted May 12, 2011 You are including product.php twice. Link to comment https://forums.phpfreaks.com/topic/236204-fatal-error-cannot-redeclare-class/#findComment-1214434 Share on other sites More sharing options...
Sanjib Sinha Posted May 12, 2011 Author Share Posted May 12, 2011 I have included Product.php in two separate files, ie; Book.php and DVD.php. Is it incorrect? But if I did not do this, how would I inherit parent Product class in two separate child class : Book and DVD? Link to comment https://forums.phpfreaks.com/topic/236204-fatal-error-cannot-redeclare-class/#findComment-1214466 Share on other sites More sharing options...
spiderwell Posted May 12, 2011 Share Posted May 12, 2011 use require_once() ? Link to comment https://forums.phpfreaks.com/topic/236204-fatal-error-cannot-redeclare-class/#findComment-1214468 Share on other sites More sharing options...
Sanjib Sinha Posted May 12, 2011 Author Share Posted May 12, 2011 Yes. You are correct. I just tried require_once in two separate files Book.php and DVD.php and it worked. Many thanks. Link to comment https://forums.phpfreaks.com/topic/236204-fatal-error-cannot-redeclare-class/#findComment-1214471 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.