Jump to content

Basic Class Interaction


Jakebert

Recommended Posts

It's 1AM, and I am tired. Someone tell me if I'm on the right track.

 

Supervisors have shifts.

Shifts have classes.

Classes have students and an instructor.

Students have levels, report cards, and parent contacts.

Instructors have qualifications and write report cards & PCs.

Supervisors can edit instructors, assign them to classes, ditto students.

 

Does that mean that I need a class for

Supervisors, shifts, classes, students, instructors, levels, report cards, parent contact, qualification, report card writer, PC writer, instructor manager, student manager, DEATH  :facewall:

 

I am confuzzled.

Link to comment
Share on other sites

I don't think you'd need a Class for each one of those but ideally.. yeah you need a class for everything

 

The key is to find a similarity between all of them.  If you can't do that then group them first. and then find similarities within those groups.  Then you can make a class for each group.  For instance

 

Supervisors, Students, Instructors and Managers are all People.

From that you have your first group and you can make a People Class.  not necessarily called People but you get the idea.

 

Then once you have made such a class you can create the other classes and just extend off of People

 

class Supervisor extends People {
blah blah
}

class Student extends People {
blah blah
}
etc,etc

Link to comment
Share on other sites

Hmm...OK, I see what you're saying. Here's my issue now.

 

Forget about instructors for now, let's focus on students. So I have a Student class

<?php
// PHP 5

//USER class
class User {
public $fname;
public $lname;
public $bday;
public $gender;
}

// STUDENT class
class Student extends User {
public $sid;
public $level;

// define methods

public function __construct($fname,$lname,$gender,$bday,$level) {
	$this->fname=$fname;
	$this->lname=$lname;
	$this->gender=$gender;
	$this->bday=$bday;
	$this->level=$level;
}

 

I also have a "front" page (the one the user sees) called add_student.php

Is this necessary? Should I just have a manage_student.php and have all the actions as methods? Here's what I have now.

<?php
// PHP 5
// Test
include 'database.inc';
include 'bizlog.php';

if(isset($_POST['submit'])) {

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$bday = $_POST['bday'];
$gender = $_POST['gender'];
$level = $_POST['level'];	

$new_student = new Student($fname,$lname,$gender,$bday,$level);

echo $new_student->fname;
echo $new_student->lname;
}

else

{

echo '<form action = "" method = "post">';
echo 'First Name: <input type = "text" name = "fname" value = "'.$fname.'"/>';
echo '<br />';
echo 'Last Name: <input type = "text" name = "lname" value = "'.$lname.'"/>';
echo '<br />';
echo 'Birthday: <input type = "text" name = "bday" />';
echo '<br />';
echo 'Male: <input type = "radio" name = "gender" value = "male"/>';
echo 'Female: <input type = "radio" name = "gender" value = "female"/>';
echo '<br />';
echo 'Level: <input type = "text" name = "level" />';
echo '<input type="submit" name = "submit" />
</form>'; 
}

?>

 

Then I have a third page that connects to the database, but I'll leave that headache for later. I'm not sure what should go into a class and what should stay on the "view" page.

Link to comment
Share on other sites

One of the pains that every beginner feels when trying to learn OOP is a lack in confidence that their class is 'right'.  How simple is too simple?  How complex is too complex?  How much should an object try to do?

 

A couple of (probably not very helpful) things to keep in mind:

 

KISS - Keep It Simple, Silly/Stupid.

Single responsibility.

 

A class'/object's purpose should be describable in one sentence, with maybe one or two words like 'and' and 'or'.  This helps keep things organized and clean, and facilitates the writing of good code.

 

So, with that said, what do you envision a manage_student.php file to look like?  And how would it differ from what you have now?

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.