Jump to content

School Database


xionhack

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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.