I see, so you would write a separate class definition for providing data from the database to the Student object. Would you do the same for the Course object?
Actually, I could just write a Populate() method (function) in my RData class (because just to use this class gives me all the database resources I need) that takes an object (just like the other RData methods) and populates that object. There are limitless ways to do this and it all depends on what you are building (mine is just an example.) For example, I could just call RData from the Student class itself. Here is some revised code to illustrate:
<?php
define("MAJOR_FRESHMAN", 1);
define("MAJOR_SOFTMORE", 2);
define("MAJOR_JUNIOR", 3);
define("MAJOR_SENIOR", 4);
class Student
{
public $ssid;
public $first;
public $last;
public $age;
public $major;
private $rdata;
function __construct()
{
$this->rdata = new RData();
}
public function update()
{
$this->rdata->update($this);
}
public function populate()
{
$this->rdata->populate($this);
}
}
class GradStudent extends Student
{
public $is_doctorate;
}
class RData
{
function __construct()
{
//connect to database
}
function insert($object)
{
if (is_a($object, "Student")) {
// write insert query and execute
} elseif (is_a($object, "GradStudent")) {
// write insert query and execute
}
}
function update($object)
{
if (is_a($object, "Student")) {
// write update query and execute
} elseif (is_a($object, "GradStudent")) {
// write update query and execute
}
}
function populate(&$object)
{
if (is_a($object, "Student")) {
$sql = "SELECT ssid, fname, lname, major FROM students WHERE ssid = " . $object->ssid;
// write code to get values from database into a variable $row (associative array)...
$object->first = $row['first'];
$object->last = $row['last'];
$object->ssid = $row['ssid'];
$object->major = $row['major'];
} elseif (is_a($object, "GradStudent")) {
// similar code
}
}
}
$student = new Student();
$student->first = "John";
$student->last = "Doe";
$student->ssid = "523-87-9134";
$student->major = MAJOR_FRESHMAN;
$student->update();
$student2 = new Student();
$student2->ssid = "523-87-9134";
$student2->populate();
echo $student2->first;
?>
Keep in mind. OOP is fundamentally a way to organize code and extend code in the hopes of freeing the programmer from more mundane structures while avoiding unnecessary repetition. Also, one of the goals is make code more modular (or portable.) This facilitates team coding and hopefully cuts down on time by implementing mashups (my definition here being numerous projects using common code objects as needed.)
That being said, object oriented code has no real advantage over procedural code for arithmetic. I mean addition is addition right? However, when you have specific calculations, OOP can be very helpful.
For example, I worked with another programmer once on a multilevel marketing (yuck) web program once. He was a big proponent of procedural programming. When he would compute a figure which had to appear on multiple pages, he'd simply copy and paste that code. What happened is that when he left and I had to maintain his code, I had to maintain all 20 pages individually. Had he used OOP practices, he could have made one function to calculate that figure and I would have only needed to maintain one function.
I know that doesn't exactly answer your question, but I just wanted to suggest that perhaps you are thinking that developing using OOP practices fundamentally changes the way you write code. You can still write your arithmetic into your SQL query if you want. You won't be breaking anything. Coding is very subjective in my opinion.
As for implementing your arithmetic into my sample model, you could simply write the profit code into the the insert() method. As for the specifics of that, you can do it many ways, but one way might be to use the ssid property of the current student object to look up courses that student has enrolled in and get prices to use. Finally, you would update some profit table or just add a profit amount as a column in the current student record you just created (again, many ways.) Since a GradStudent insert would be almost identical with respect to profit, you would write the calculation code as separate methods in the same RDate class to avoid repetition (again, you don't have to do it this way.)