0

Ok, I couldn't insert the overview image, but I'm required to create a PHP app for my studies, it must allow student to register, and the administrator to edit course and student info. We are not required to code it in an OOP style, but since its the best programming practice to code in OOP, why not learn OOP from the beginning.

I'm a beginner but I know the basics about OOP, classes, inheritance, setters and getters and all that cool lingo, but I'm struggling to decide which parts of this app should be objects, should I make course and student classes or add, edit and delete classes? Any advice on how to approach and visualize such a problem would be appreciated.

grasshopper
  • 1,381
  • 4
  • 19
  • 36
  • 1
    "its the best programming practice to code in OOP" - says who? – Oliver Charlesworth Mar 25 '12 at 18:53
  • Read these 2 books http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882 and http://www.amazon.com/Applying-Introduction-Object-Oriented-Iterative-Development/dp/B003MV55EA/ref=sr_1_76?s=books&ie=UTF8&qid=1332549040&sr=1-76 – Songo Mar 25 '12 at 18:54
  • @OliCharlesworth Ok, in the books I received it explained that most languages is heading in an OOP direction, it may not be best practices, but rather a good addition to my skills. – grasshopper Mar 25 '12 at 18:58
  • It's not necessarily "best programming practice to code in OOP". OOP is good for certain things, definitely not for all. – Madara's Ghost Mar 25 '12 at 19:00
  • I also found this post helpful - http://stackoverflow.com/questions/4805368/oop-approach-in-php – grasshopper Mar 25 '12 at 19:32

4 Answers4

5

Very roughly: This is how I would do it:

  1. Store your data in SQL or XML. You will need two SQL tables, one for Students and one for Courses. You can use one XML file containing all the data, or you can use two files (which I recommend).

  2. Create a class called, for example, dataItem with a property like '$arr_fields' corresponding to a single data record (a single row in a SQL table, or an XML record).

  3. The dataItem class should have the following methods (all public):

    • loadFromSQL() (or loadFromXML())
    • saveToSQL() (or saveToXML())
    • add(), edit() and delete()
    • a view() method using HTML
    • These methods are obviously used to read and write data between the SQL/XML data and $arr_fields of the class, and to display the data in $arr_fields. The keys of $arr_fields are the SQL column names (or XML tag or attribute names) for the specific SQL table.
  4. Try not to call loadFromSQL() or saveToSQL() in your constructor or in any of the other methods which are used to modify only the class data. Keep these actions separate. EDIT: This is a personal preference which helps me to keep track of the state of my objects.

  5. Create Student and Course classes that extends the dataItem class.

  6. You can override methods, for instance the view() method, inside your extended classes if you need to.

  7. Then you can call the methods in Students and Courses from an Admin object (like rcdmk suggested) or maybe from StudentFolder and CourseFolder classes whose view() method contains buttons for the actions that need to be performed. (Let StudentFolder and CourseFolder extend a Folder class that you create).

UPDATE:

For example: If your primary key in a SQL table is id, then dataItem's loadFromSQL($id, $tablename) should set $arr_fields so that its keys are the column names and it's values are the values from the row whose primary value is equal to $id.

In Students, you can then override loadFromSQL() as follows:

class Students extends dataItem {

  // other attributes 

   public function loadFromSQL($id) {
     parent::loadFromSQL($id, "Students");
   }

}

EDIT: On reconsideration, it might be better to set $arr_fields["id"] = $id and also set $tablename with the constructor for dataItem - then you never have to override loadFromSQL() or specify parameters for it. loadFromSQL() should then load the record if it exists. saveToSQL() should save $arr_fields in SQL if $arr_fields["id"] is set and create a new record if it is not set. Anyway, you must find a consistent way of interacting with the data which works for you, these are just possibilities.

However, if you are not experienced with OOP and SQL or XML, you might be opening a can of worms for yourself and it might be better to just do your assignment using functions only and php arrays for your data. Unless you have some time to learn...

Stefan
  • 3,850
  • 2
  • 26
  • 39
  • Ok, I think I get it. dataItem will be the parent class with the other two extending from that one, but can you explain 4., do you mean that I should create those functions in the student and course classes, how should these actions be seperate? – grasshopper Mar 25 '12 at 19:30
  • You don't have to recreate those methods in the extended classes - this is the point of inheritance, you only need to define them once, in the parent class. What I mean by keeping the actions separate: In your code and in your thinking, keep the methods which operate on the class data, separate from the methods which operate on the SQL or XML data. So for example, when you instantiate a Student object and want to fill it with data from a specific SQL row, you call the constructor (which constructs an 'empty' object in terms of its data) and then call loadFromSQL() separately AFTER that. – Stefan Mar 25 '12 at 19:39
  • Ok, I think I understand what you mean, thanks I appreciate it and I will likely reread this page a couple of times. – grasshopper Mar 25 '12 at 19:52
  • OK, but think carefully if you feel able to handle the assignment with all this. Maybe rather try this when you are not under pressure...? I won't be able to help you with details after today. Also note that I reconsidered my advice regarding recreating the methods - you can recreate them (i.e. override them) but you don't have redefine the code in the parent class - see my answer update. – Stefan Mar 25 '12 at 20:01
  • Challenge accepted - I have to learn this if I want to build my empire one day, If I get stuck I will boil the kettle make some coffee and prepare for an all-nighter. – grasshopper Mar 25 '12 at 20:17
2

From a simple perspective:

Abstract the main objects as classes and use methods for actions of this objects:

Student (object) are deleted (action) by the Admin (object), so

Admin class will have a deleteStudent method, because Admin deletes Students.

Another aprouch is to concentrate all Student related actions in the Student class:

Student class will have a public delete method that Admin can use.

Anyone that think this in better ways of explanation can edit this wiki.

Ricardo Souza
  • 16,030
  • 6
  • 37
  • 69
2

Think about which aspects of your system are actually objects, you know, something you can do something with. The methods are what you do to the objects. So, you're on the right track with course and student classes, and add, edit, and delete would be methods of those classes.

But don't get too bogged down with it. If it's not your core assignment objective, you could quickly get in over your head by trying to do everything exactly the right way. If you can formulate a clear way to get to where you need to go, then go for it, if it seems confusing, back off it a little and learn some more.

Jason
  • 13,606
  • 2
  • 29
  • 40
2

You say that you know the basis of OOP, however you ask whether you should create Course, Student classes OR Add, Delete, Edit classes. Well maybe there are other practices, but I guess the most popular one and the only I am aware of is to use nouns as classes and verbs as their methods. Hence, intuitively there is something wrong with class "Add" or "Edit". What I would have done if I were were, is to think of all "entities" that might be considered an object - like Student, Course, Lecturer, Class (Room) and depending on how advanced your model should be you can add more like Building etc. Then try to implement basic things like creating new student, registering for a course, associating teacher with a course etc. Once you have it in place and IT IS WORKING you might want to add advanced things, like inheritance. For example you might want to say, that both Teacher and Student are a Person so you might want to create such abstract class and use inheritance.

mkk
  • 7,583
  • 7
  • 46
  • 62