Annoying statics in php

I had never thought too much about how php handles static methods and properties in classes until I started fiddling with my activerecord again today. I had run into problems before but after less than satisfactory responses from the people writing people (you aren't doing OO right, no other language does this, it has to be done at compile time, blah blah blah) I simply did workarounds where static methods/properties and inheritence should have been used. Bottom line? Php does NOT support inheritence with statics. You can't even fake it. I'm online, I read alot, I'd run across all this zend framework hype and seem some of the code you could supposedly use. I have a version of activerecord that required an instance to call find methods - worked really nifty, returned a collection class that lazy created activerecord instances as they were accessed - really rather cool, anyway. So I decided maybe there was some way to get static calls to those find methods working, after all, zend managed it, right??? To make a long story short... /start rant/ I couldn't get it to work, not with reflection, not with debug_backtrace tricks...it just refused to cooperate. After reading around the internet about the "zend framework" version of active record and seeing the "psuedocode" it made me irritated - there was no way their code was possible in php currently. I find it annoying that zend is pushing this whole framework thing and showing examples that aren't possible. Bottom line? I was right, the code from the webcast was vaporware *proceeds to laugh in very evil voice* - and if they were just showing what they "hoped the final syntax would be" then they damn well should have SAID SO /end rant/ So in the end bitched to my brother about it ( and he blogged about it ) Anyway, I reverted back to my original work (look at the comments in the post linked above, zend has done the same) - forcing an instance for find calls instead of allowing static calls - a nice side effect is this makes sure my constructor gets called every time (sets up the db stuff really niftily) I hope they do eventually fix the problem. Right now the static stuff is basically useless for any kind of extended class. I find it rather annoying that they haven't fixed the problem BEFORE now - after all, the bugs (marked bogus or wont fix because obviously the zend guys weren't using php5 at that point if they didn't see the problem until zend framework came along) the bugs/requests have been around for a LONG TIME Here's a list :) get_class() with no argument, always return the original classname Incoherent behaviour Special keyword 'self' inherited in child classes Inheritance of private static variables fails (related to self:: ) Changing a static variables in a class changes it across sub/super classes Inheritance of static functions get_class(self) for static methods Now most of these are marked bogus (erroneously, they should have been wont fix) or wontfix which is just annoying. And I've noticed that any comments in the documentation for statics that link to these bugs disappear... can we just get a way to get the calling class in a static method? is that so dang hard? And I'm one who would love __call to work on statics and have static constructors...that would really make my day... I believe the original place I ran into this problem was with a singleton class that any class you wanted to keep as a singleton would extend - you'd have a factory method that would fetch the stored instance for each one simply by extending it - but since you couldn't stick all the code into one spot because you couldn't figure out what child was calling the parent static - you were screwed or had to do too much cut and paste... so I had to move to a factory class to keep track of all the instances (thus tying the framework portion of my stuff together more tightly, something I'd wanted to AVOID doing) As to actually fixing the problem? We'll see how all this pans out...after all, it's only been what, January 2004 or so? Two years isn't too long to ask to have to stop doing workarounds and have static methods actually use inheritence...

Comments

Building a culture of objects in PHP | Professional PHP

[...] Elizabeth Marie Smith does too and adds: I find it rather annoying that they haven’t fixed the problem BEFORE now - after all, the bugs (marked bogus or wont fix because obviously the zend guys weren’t using php5 at that point if they didn’t see the problem until zend framework came along) the bugs/requests have been around for a LONG TIME [...]

2006-01-12 9:44 pm

Peter Hodge

Several months ago I filed my own slightly different feature request to solve the same issue using magic constants - I am hoping that the idea of adding extra magic constants is more appealing to the developers than adding a new keyword to the parser and they won't shrug it off. You may wish to stop by and add a vote or comment: http://bugs.php.net/bug.php?id=34804.

regards,
Peter

2006-01-16 8:06 pm

ramblinations

php singles bar limited to one generation only,...

see, i thought it was just me. i thought, well, gemma, you probably shouldn't be trying to inherit from base singletons anyway. you're not really a programmer, what do you know? go play with bash, you'll feel better. (incidentally, i did it anyway a...

2006-04-24 7:06 pm

Building a culture of objects in PHP « Resistance is futile

[...] Elizabeth Marie Smith does too and adds: I find it rather annoying that they haven’t fixed the problem BEFORE now - after all, the bugs (marked bogus or wont fix because obviously the zend guys weren’t using php5 at that point if they didn’t see the problem until zend framework came along) the bugs/requests have been around for a LONG TIME [...]

2007-09-12 11:08 am

Post a Reply