- Jan 23, 2008
- 1,166
- 0
- 76
Well, I thought I was decent at PHP. My background is Java, where multiple constructors are allowed, and polymorphism is not an illusion.
Anyway, I tried to declare two __construct() functions, but PHP freaked out at me, and told me that I can't redeclare functions. I thought about using __call(), but doing so, I always got to a point where I needed to be able to say something like $day = new Day($args)- which would want to call a constructor, and send me back into the __call function. I ended up going back and replacing the existing new Day($args) statements with mkDay($args), and having __call() pick up the mkDay() calls and do the heavy lifting, then call a dummy constructor:
function __call($function, $args)
{
if($function == "mkDay")
{
//the first case represents the constructor for weekoffset, day, and session
if(count($args) == 3)
{
$day = new Day();
$day->day = $day;
$day->page = Webpage::getThrowAway();
$week = mktime(0, 0, 0, date("n"), date("j"), date("Y")) - (date("N")*3600*24);
$sundate = getdate($week);
$day->weekoffset = $weekoffset;
$day->session = $session;
$day->startTimeStamp = mktime(0, 0, 0, $sundate['mon'], ($sundate['mday']+$day)+7*$this->weekoffset, $sundate['year']);
$day->startDateTime = date('Y-m-d H:i:s A', $this->startTimeStamp);
$day->db = Database::getConnection();
}
//the second case represents the constructor for month, day, year, and session
else if($count($args) == 4)
{
$day = new Day();
$day->startTimeStamp = mktime(0,0,0,$month,$day,$year);
$day->startDateTime = date('Y-m-d H:i:s A', $this->startTimeStamp);
$day->session = Session:
ullFromSession();
return $day;
}
}
else echo "You've attempted to call '$function', which does not exist, in class Day.";
}
(apologies for poorly formatted code; if anandtech forums have some kind of code tag, I am unaware of it).
All in all, this is an extremely roundabout way to go about things. Does anyone have any advice on a better way to do polymorphism in PHP?
Anyway, I tried to declare two __construct() functions, but PHP freaked out at me, and told me that I can't redeclare functions. I thought about using __call(), but doing so, I always got to a point where I needed to be able to say something like $day = new Day($args)- which would want to call a constructor, and send me back into the __call function. I ended up going back and replacing the existing new Day($args) statements with mkDay($args), and having __call() pick up the mkDay() calls and do the heavy lifting, then call a dummy constructor:
function __call($function, $args)
{
if($function == "mkDay")
{
//the first case represents the constructor for weekoffset, day, and session
if(count($args) == 3)
{
$day = new Day();
$day->day = $day;
$day->page = Webpage::getThrowAway();
$week = mktime(0, 0, 0, date("n"), date("j"), date("Y")) - (date("N")*3600*24);
$sundate = getdate($week);
$day->weekoffset = $weekoffset;
$day->session = $session;
$day->startTimeStamp = mktime(0, 0, 0, $sundate['mon'], ($sundate['mday']+$day)+7*$this->weekoffset, $sundate['year']);
$day->startDateTime = date('Y-m-d H:i:s A', $this->startTimeStamp);
$day->db = Database::getConnection();
}
//the second case represents the constructor for month, day, year, and session
else if($count($args) == 4)
{
$day = new Day();
$day->startTimeStamp = mktime(0,0,0,$month,$day,$year);
$day->startDateTime = date('Y-m-d H:i:s A', $this->startTimeStamp);
$day->session = Session:
return $day;
}
}
else echo "You've attempted to call '$function', which does not exist, in class Day.";
}
(apologies for poorly formatted code; if anandtech forums have some kind of code tag, I am unaware of it).
All in all, this is an extremely roundabout way to go about things. Does anyone have any advice on a better way to do polymorphism in PHP?
