• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

PHP polymorphism problems

presidentender

Golden Member
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?

 
within your 2nd __constuct function, use: "parent::__construct" (I think...this might do all the actions in the parent's construct function instead of overriding it, however).
 
In php, can you have a ctor that takes null args?
If so, maybe have a constructor that takes all possible params and does one thing if they are both supplied and something else if an arg is null, empty, etc.

In java, you could even throw in a boolean param indicating how you want the constructor to handle the other params. Hacky, yes. So document it..
 
Originally posted by: trexpesto
In php, can you have a ctor that takes null args?
If so, maybe have a constructor that takes all possible params and does one thing if they are both supplied and something else if an arg is null, empty, etc.

In java, you could even throw in a boolean param indicating how you want the constructor to handle the other params. Hacky, yes. So document it..

In Java, I'd get real polymorphism, and this would never have been a problem to begin with. I ended up reengineering the thing so I only needed one constructor and doing all the math outside.
 
Back
Top