xionhack Posted April 7, 2010 Share Posted April 7, 2010 Hello. I am trying to make a web application for something that works as a school. The points are: The students can become teachers later on. There are only 20 subjects that the teacher should teach. I want to record, which of those subjects can the teacher teach, and for the students, which subjects have they studied, with who, and which other subjects they have to study. I am new at object oriented programming, so I was trying to do it with classes so I could learn more, but I think Im not doing it correctly. Can somebody explain to me which approach should I take? Thanks! Link to comment https://forums.phpfreaks.com/topic/197898-school-database/ Share on other sites More sharing options...
br0ken Posted April 7, 2010 Share Posted April 7, 2010 That's a pretty vague question but here goes... You need to create a class for each entity. I was always taught you should have a class for each noun (this is only mostly true). For example, you would need a Teacher class and a Student Class. You would Also need a Lesson class. Hopefully this will help but if not, try and be more specific with your question. Link to comment https://forums.phpfreaks.com/topic/197898-school-database/#findComment-1038546 Share on other sites More sharing options...
xionhack Posted April 7, 2010 Author Share Posted April 7, 2010 I have a question about that. A student, can be a teacher, So i was thinking about making only one table with boths infos, do i still have to make 2 classes Link to comment https://forums.phpfreaks.com/topic/197898-school-database/#findComment-1038672 Share on other sites More sharing options...
ignace Posted April 8, 2010 Share Posted April 8, 2010 A student, can be a teacher You'll have to think this through very carefully. IMO should you create a separate table for both teacher and student because a student is not a teacher although data may be in some circumstances similar (name, birthdate) between both, a student does not have an employment record (in normal circumstances) nor does the teacher get graded. In OOP terms it also does not make any sense as: a student is-a teacher OR a teacher is-a student class Student extends Teacher {} class Teacher extends Student {} In the first scenario is a student capable of more then the teacher while in the other you would have an overhead (getGrades(), getTimesFlunked(), ..) functions that make no sense in a Teacher context. In OOP it is a good practice to use Value Objects (or Active Records) class Student {} class Teacher {} Consider for example: public function setGrades(Teacher $t, Student $s, array $grades) Now assume you said a Student is-a Teacher which assumes this is equally true: public function setGrades(Student $t, Student $s, array $grades) And to prevent this from happening you would always have to write: if ($t instanceof Teacher) Link to comment https://forums.phpfreaks.com/topic/197898-school-database/#findComment-1038852 Share on other sites More sharing options...
br0ken Posted April 8, 2010 Share Posted April 8, 2010 Ignace is correct that a Student and Teacher are separate entities and therefore both need a separate class, but you are also right in noticing they share a common factor; they are both a human! The best way to do this would be to have a base class Human (or Person) and have both Teacher and Student extend from them. <?php abstract class Human { protected $_name = ''; protected $_gender = ''; protected $_role = ''; public function getName() { return $this->_name; } } class Teacher extends Human { public function __construct($name) { $this->_name = $name; $this->_role = 'Teacher'; } } class Student extends Human { public function __construct($name) { $this->_name = $name; $this->_role = 'Student'; } } $teacher = new Teacher('Mrs Smith'); $student = new Student('Jack Adams'); echo $teacher->getName(); //Returns Mrs Smith echo $student->getName(); // Returns Jack Adams ?> The benefit of this method is that both Teacher and Student can use the Human functions. This will save you duplicating code between the two objects. Link to comment https://forums.phpfreaks.com/topic/197898-school-database/#findComment-1039097 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.