Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

[SOLVED] php class inheritance

class inheritance

  • Please log in to reply
3 replies to this topic

#1 Dorgon


    CC Regular

  • Member
  • PipPipPip
  • 37 posts

Posted 08 October 2012 - 07:53 AM

Good evening programmers,

I have a question about inheritancein php.

Let's say I have a class User, it implements IUserDAO(interface):

class user implements IUserDAO{

And I have a class Student, it extends user and implements IStudentDAO:

class student extends user implements IStudentDAO{

And i have a class named employee, it extends also user and it implements IEmployeeDAO:
class employee extends user implements IEmployeeDAO{

now, to use inheritance I need to use the user class from the student and Employee class.
To do that i need to include or reqire the file user.php, otherwise it says can not find class user...

But when I include the file user.php from multiple files (employee.php and Student.php) it says: cannot redeclare class user in:....

how do I solve this without putting all classes in 1 file?

I hope my question is clear, thanks in advance!
  • 0

#2 Vaielab


    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 08 October 2012 - 08:06 AM

To include (or require) the user.php file use include_once or require_once
Your problem was that you included the file user.php twice (or more) so php tried to recreate the user class each time.
But with include_once or require_once php will include the file only once, even if you call this function thousand of time.
  • 2

You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !

#3 Dorgon


    CC Regular

  • Member
  • PipPipPip
  • 37 posts

Posted 08 October 2012 - 08:14 AM

Thank you! it works now!
  • 0

#4 Orjan


    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 08 October 2012 - 08:19 AM

This topic has been marked as SOLVED. If you have a similar question or topic, you can go back to the subforum and start a new topic to continue discussions.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.

Also tagged with one or more of these keywords: class, inheritance

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download