Keith Watanabe * NET 2.0

static data members and inheritance in PHP
By: Keith Watanabe
Published On: 10-9-2007

i found something interesting the other day using PHP.  let's say you have a base class (which would be using the singleton design pattern) with a static data member called $instance.  later, you inherit the base class with another two additional classes and have them utilize the $instance data member  (say through setting it in a method called getInstance()).  originally, i had thought that each instantiating class would get its own copy of $instance.  nope!!!  because the data member is static in the base class,  it really only pertains to the base class, especially when you start referring to it as self::$instance.

i'm not sure if this is a good explanation of the situation, but i learned a truly valuable lesson today: be careful when using static data members and inheritance.  If you intend to do some sort of polymorphism with the static data member, it probably won't behave as you'd expect.

AddThis Social Bookmark Button Sphere: Related Content

Trackbacks: (Trackback URL)

No Comments Posted Yet
July [August] September
Sun Mon Tue Wed Thu Fri Sat
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6