OOP php - test a class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Brad1352
Forum Newbie
Posts: 2
Joined: Sun Jun 08, 2008 11:58 am

OOP php - test a class

Post by Brad1352 »

I have a simple class called Student.

The Student class has instance variables for:
- student number
- birthdate

The constructor has both of these values as
parameters.

There are also getStudentNumber(), setStudentNumber(),
getBirthdate(), setBirthdate(), and getAgeInYears()
public methods.

Please help me write a very simple test.

I don't need all the set/get methods. please just write me an example of one get and one set + constructor test.

I have no idea what to do.

Thanks
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: OOP php - test a class

Post by hansford »

<?php

class Student {

var $birthdate;
var $student_number;

function Student($student_number, $birthdate){

$this->student_number = $student_number;
$this->birthdate = $birthdate;
}

function getStudentNumber(){

return $this->student_number;
}

function setStudentNumber($student_number){

$this->student_number = $student_number;
}
}

$student = new Student("789-51-0123","01/11/88");

echo $student->getStudentNumber();
?>
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: OOP php - test a class

Post by hansford »

This might make it more understanding - I also added a set function

<?php

class Student {

var $birthdate;
var $student_number;

function Student($student_number, $birthdate){

$this->student_number = $student_number;
$this->birthdate = $birthdate;
}

function getStudentNumber(){

return $this->student_number;
}

function setStudentNumber($student_number){

$this->student_number = $student_number;
}
}

//call our constructor
$student = new Student("789-51-0123","01/11/88");

echo "Student number set in constructor call: " . $student->getStudentNumber() . "<br>";

//set a new student number
$student->setStudentNumber("689-61-7890");

echo "Student number after setStudentNumber(): " . $student->getStudentNumber();

//you can also call a get function like this:
//$student_number = $student->getStudentNumber();
//the varibale $student_number now holds the returned value
?>
Brad1352
Forum Newbie
Posts: 2
Joined: Sun Jun 08, 2008 11:58 am

Re: OOP php - test a class

Post by Brad1352 »

Thanks,

I forgot to mention: the kind of test that I'm interested in is this:

http://www.lastcraft.com/simple_test.php

and that's because we need to check every entry for those fields.
Post Reply