0

I would like to set up a foundation of classes for an application, two of which are person and student. A person may or may not be a student and a student is always a person. The fact that a student “is a” person led me to try inheritance, but I can't see how to make it work in the case where I have a DAO that returns an instance of person and I then want to determine if that person is a student and call student related methods for it.

class Person {
    private $_firstName;

    public function isStudent() {
        // figure out if this person is a student
        return true; // (or false)
    }
}

class Student extends Person {
    private $_gpa;

    public function getGpa() {
        // do something to retrieve this student's gpa
        return 4.0; // (or whatever it is)
    }
}

class SomeDaoThatReturnsPersonInstances {
    public function find() {
        return new Person();
    }
}

$myPerson = SomeDaoThatReturnsPersonInstances::find();

if($myPerson->isStudent()) {
    echo 'My person\'s GPA is: ', $myPerson->getGpa();
}

This obviously doesn't work, but what is the best way to achieve this effect? Composition doesn't sond right in my mind because a person does not “have a” student. I'm not looking for a solution necessarily but maybe just a term or phrase to search for. Since I'm not really sure what to call what I'm trying to do, I haven't had much luck. Thank you!

Tim
  • 97
  • 1
  • 7
  • You have overridden `isStudent()` in Student, right? – kennytm Apr 03 '10 at 13:33
  • I could, yes. In the Student class, isStudent() would always be true. If I have an instance of the base Person class, isStudent() may or may not be true. – Tim Apr 03 '10 at 17:03

1 Answers1

0
<?php
class Person {
    #Can check to see if a person is a student outside the class with use of the variable
    #if ($Person->isStudentVar) {}
    #Or with the function
    #if ($Person->isStdentFunc()) {}

    public $isStudentVar = FALSE;  

    public function isStudentFunc() {
        return FALSE;
    }
}

class Student extends Person {
    #This class overrides the default settings set by the Person Class.
    #Also makes use of a private variable that can not be read/modified outside the class

    private $isStudentVar = TRUE;  

    public function isStudentFunc() {
        return $this->isStudentVar;
    }

    public function mymethod() {
        #This method extends the functionality of Student
    }
}

$myPerson1 = new Person;
if($myPerson1->isStudentVar) { echo "Is a Student"; } else { echo "Is not a Student"; }
#Output: Is not a Student

$myPerson2 = new Student;
if($myPerson2->isStudentFunc()) { echo "Is a Student"; } else { echo "Is not a Student"; }
#Output: Is a Student
?>

I would choose one way and stick to it. Just deomonstrating various ideas and techniques.

bigstylee
  • 1,240
  • 1
  • 12
  • 22
  • Thanks for your reply. The isStudentFunc() in the base Person class won't always return false though. If it returns true, I would want to be able to do something like this: $myPerson1 = new Person(); if($myPerson1->isStudentFunc()) { $myPerson1->mymethod(); } – Tim Apr 03 '10 at 17:02