h1

At Last, a Use for Late Static Bindings!

June 18, 2014

I had a base class called Bee. From that I was deriving Queen Bee, Worker Bee, etc. The only difference between the derived classes where certain constants are used to initialise certain data members. That being the case, it would be useful if the constructor on the base class did all the work. The class constants on the dreived classes need to be passed back to the constructor on the base class. Since constructor in PHP have to chain up the hierarchy manually, using parent:__construct() , this can be done by passing jn the derived class constants to the parameter list. However, that is almost as repetitive as giving the derived classes their own constructors. Trying to do it it all in the base class constructor using self:: doesn’t work…because self:: isn’t forward…it tries to pull constants from the base class itself,. For the constructor on the base class to pull in constants from the derived class that chained back ti it, you need forward bindings, which means replacing self:: with static:: in the base class constructor.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: