forum.webdiplomacy.net

webDip dev coordination forum / public access todo list
It is currently Sat Nov 25, 2017 1:44 am

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Thu Jun 24, 2010 7:05 pm 
Offline

Joined: Wed Jul 29, 2009 10:22 am
Posts: 841
Just a quick question:

When do I need to use
Code:
function __construc() {
 parent::__construct();
}

in a class?

Maybe this has something to do with the recent errors on my site.

Oliver


Top
 Profile  
 
PostPosted: Fri Jun 25, 2010 4:41 am 
Offline
Site Admin

Joined: Sat Jun 28, 2008 6:24 am
Posts: 892
Well if you override a constructor the parent contructor (the constructor in the superclass) won't get called without explicitly doing so

If you don't define any constructor the parent's constructor will be used by default, so there's no need to add that code in by itself since it's effectively done automatically, but if you do want to extend the functionality of constructor code (rather than replace it) you'll usually want to include a call to the parent constructor before or after you've run the extra code


Top
 Profile  
 
PostPosted: Fri Jun 25, 2010 8:19 am 
Offline

Joined: Wed Oct 08, 2008 12:47 pm
Posts: 726
A time you might want to do it would be if your custom constructor contained just a very slight change (such as changing a variable) from the main code. Then, you would have the constructor as a two line function, one changing the variable then the other calling the parental code. This saves you rewriting the whole function, and also helps with compatibility in the future, as changes to the parental code will still be inherited.


Top
 Profile  
 
PostPosted: Fri Jun 25, 2010 10:50 am 
Offline

Joined: Wed Jul 29, 2009 10:22 am
Posts: 841
I was wondering, because in the example code you use:
Code:
class BuildAnywhereVariant_OrderInterface extends OrderInterface {

   /**
    * Call the parent constructor transparently to keep things working
    */
   public function __construct($gameID, $variantID, $userID, $memberID, $turn, $phase, $countryID,
      setMemberOrderStatus $orderStatus, $tokenExpireTime, $maxOrderID=false)
   {
      parent::__construct($gameID, $variantID, $userID, $memberID, $turn, $phase, $countryID,
         $orderStatus, $tokenExpireTime, $maxOrderID);
   }

or
Code:
class CustomStartVariant_processGame extends processGame {
   public function __construct($id)
   {
      parent::__construct($id);
   }


Oliver


Top
 Profile  
 
PostPosted: Fri Jun 25, 2010 4:51 pm 
Offline

Joined: Wed Oct 08, 2008 12:47 pm
Posts: 726
Strange. To the best of my knowledge that code is excessive, although I guess it helps with readability.


Top
 Profile  
 
PostPosted: Sat Jun 26, 2010 2:49 am 
Offline
Site Admin

Joined: Sat Jun 28, 2008 6:24 am
Posts: 892
Sleepcap wrote:
I was wondering, because in the example code you use:
Code:
class BuildAnywhereVariant_OrderInterface extends OrderInterface {

   /**
    * Call the parent constructor transparently to keep things working
    */
   public function __construct($gameID, $variantID, $userID, $memberID, $turn, $phase, $countryID,
      setMemberOrderStatus $orderStatus, $tokenExpireTime, $maxOrderID=false)
   {
      parent::__construct($gameID, $variantID, $userID, $memberID, $turn, $phase, $countryID,
         $orderStatus, $tokenExpireTime, $maxOrderID);
   }

That's for the $maxOrderID=false part, which the parent constructor requires but which the subclass constructor allows you to leave and it'll give a default value

Quote:
Code:
class CustomStartVariant_processGame extends processGame {
   public function __construct($id)
   {
      parent::__construct($id);
   }


Oliver

Not sure why I put this in there, you could delete it and it wouldn't make a difference


Top
 Profile  
 
PostPosted: Sat Jun 26, 2010 3:17 am 
Offline

Joined: Wed Oct 08, 2008 12:47 pm
Posts: 726
Oh right.
How come the default class doesn't have the default = false written in as well?


Top
 Profile  
 
PostPosted: Sat Jul 03, 2010 6:37 am 
Offline
Site Admin

Joined: Sat Jun 28, 2008 6:24 am
Posts: 892
No idea, probably wasn't a conscious decision. OrderInterface was part of the mad rush to get JS orders implemented to ease server load so there are probably loads of relics of a lack of detailed design lurking around there :(


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group