dawemu Posted November 4, 2008 Share Posted November 4, 2008 HI all. i'm a beginner to PHP. so i need some help from you to this problem. actually i dont know how to create overload constructors. so please help me to correct this code. Thanks <?php /* * Class Name - Search Class * Author - Damith Mulleriyawa * Date - 04/11/2008 * Version - 1.0 * Description - Search controller */ class Search { //private attributes for the class private $studentID; private $studentFirstName; private $studentLastName; private $date; private $timeIn; private $timeOut; private $staffID; private $staffFirstName; private $staffLastName; //defult constructor public function __construct() { $num = func_num_args(); $args = func_get_args(); switch($num){ case 0: $this->__call('__construct0', null); break; case 1: $this->__call('__construct1', $args); break; default: throw new Exception(); } } } //constructor public function __construct1($studentID,$studentFirstName,$studentLastName, $date, $timeIn, $timeOut) { $this->studentID = $studentID; $this->studentFirstName = $studentFirstName; $this->studentLastName = $studentLastName; $this->date = $date; $this->timeIn = $timeIn; $this->timeOut = $timeOut; } public function __construct1($staffID, $staffFirstName, $staffLastName) { $this->studentID = $studentID; $this->staffFirstName = $staffFirstName; $this->staffLastName = $staffLastName; } public function getStudentID() { return $this->studentID; } public function setStudentID($studentID) { $this->studentID = $studentID; } public function getStudentFirstName() { return $this->studentFirstName; } public function setStudentFirstName($studentFirstName) { $this->studentFirstName = $studentFirstName; } public function getStudentLastName() { return $this->studentLastName; } public function setStudentLastName($studentLastName) { $this->studentLastName = $studentLastName; } public function getDate() { return $this->date; } public function setDate($date) { $this->date = $date; } public function getTimeIn() { return $this->timeIn; } public function setTimeIn($timeIn) { $this->timeIn = $timeIn; } public function getTimeOut($timeOut) { return $this->null; } public function setTimeOut($timeOut) { $this->timeOut = $timeOut; } public function getStaffID() { return $this->staffID; } public function setStaffID($staffID) { $this->staffID = $staffID; } public function getStaffFirstName() { return $this->staffFirstName; } public function setStaffFirstName($staffFirstName) { $this->staffFirstName = $staffFirstName; } public function getStaffLastName() { return $this->staffLastName; } public function setStaffLastName($staffLastName) { $this->staffLastName = $staffLastName; } }//end of class search ?> Quote Link to comment https://forums.phpfreaks.com/topic/131297-php-overload-constructors/ Share on other sites More sharing options...
corbin Posted November 4, 2008 Share Posted November 4, 2008 When you say overloading, do you mean PHP overloading, or true overloading? Quote Link to comment https://forums.phpfreaks.com/topic/131297-php-overload-constructors/#findComment-681801 Share on other sites More sharing options...
roopurt18 Posted November 4, 2008 Share Posted November 4, 2008 AFAIK, in PHP 5 and less you can't overload a function in the true OOP sense. You can provide optional parameters or shove a bunch of configuration parameters into a single array parameter though. Perhaps you can in PHP6 but I'm uninformed about that. Quote Link to comment https://forums.phpfreaks.com/topic/131297-php-overload-constructors/#findComment-681873 Share on other sites More sharing options...
JonnoTheDev Posted November 4, 2008 Share Posted November 4, 2008 Seems like a strange design this. Wouldnt you be better splitting the staff and students into polymorphic classes defined by abstraction or an interface? Quote Link to comment https://forums.phpfreaks.com/topic/131297-php-overload-constructors/#findComment-681991 Share on other sites More sharing options...
rhodesa Posted November 4, 2008 Share Posted November 4, 2008 yeah, by the looks of it, it seems like a base 'person' class (or abstract class) would be best, then extend that to add the needed functionality that is specific to staff and student. Quote Link to comment https://forums.phpfreaks.com/topic/131297-php-overload-constructors/#findComment-682013 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.