NightCabbage Posted March 5, 2009 Share Posted March 5, 2009 Hey everyone Just got an odd problem here... I've set up a class, as the example below, and it seems to be ignoring the use of the "static" keyword completely. <?php class Happy { static function printBlah() { echo "Blah"; } function printMoo() { echo "Moo"; } } Happy::printBlah(); Happy::printMoo(); $pie = new Happy(); $pie->printBlah(); $pie->printMoo(); ?> Output: BlahMooBlahMoo I'd expect the output to be something like: Blah[error][error]Moo Any idea what's going on here? I'm probably just doing something stupid (am quite tired)... Thanks! Quote Link to comment Share on other sites More sharing options...
WolfRage Posted March 5, 2009 Share Posted March 5, 2009 So I tired the same thing and no error. So i am thinking this must be a bug or they have done it purposely, not sure which. But as you can see this person did the same thing and posted it to the manual. http://us3.php.net/manual/en/language.oop5.static.php#88394 Quote Link to comment Share on other sites More sharing options...
btherl Posted March 5, 2009 Share Posted March 5, 2009 From http://us3.php.net/manual/en/language.oop5.static.php "A member declared as static can not be accessed with an instantiated class object (though a static method can)." A static method is very different from a static member. A method is still the same method whether it is called statically or from an instance. Members are different because they require storage space, and they must be stored either globally (static) or per-instance (non-static). Quote Link to comment Share on other sites More sharing options...
NightCabbage Posted March 5, 2009 Author Share Posted March 5, 2009 A static method is very different from a static member. A method is still the same method whether it is called statically or from an instance. Members are different because they require storage space, and they must be stored either globally (static) or per-instance (non-static). Thanks btherl I'm from a programming background, so I didn't expect this behaviour! So there's no difference between a static and a non-static function? (other than for readability and human reference?) (how do you mark a topic as solved?) Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted March 6, 2009 Share Posted March 6, 2009 So there's no difference between a static and a non-static function? (other than for readability and human reference?) And the fact that you can call a static method without needing to instantiate the class. Quote Link to comment Share on other sites More sharing options...
btherl Posted March 6, 2009 Share Posted March 6, 2009 Yep, PHP treats it as an advisory thing, not as a declaration of how you will use it. PHP is a bit loose in its enforcement of things like this. It enforces things when it is forced to (because it affects implementation), but lets a lot of other things go. Mark, you can call any method in php without instantiating the class, but php will generate a warning if it's not declared static. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.