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
https://forums.phpfreaks.com/topic/236204-fatal-error-cannot-redeclare-class/
Share on other sites

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.