roopurt18 Posted April 26, 2007 Share Posted April 26, 2007 Would you call it bad practice to expose "constants" through a "namespace" using the class keyword? <?php class Module { var $CONST1 = 1, $CONST2 = 2, $CONST3 = 3; } // Elsewhere in the code switch($val){ case $Module::CONST1: break; case $Module::CONST2: break; case $Module::CONST3: break; } ?> I can think of a few reasons not to do it, but the usage just appears more object oriented. Quote Link to comment https://forums.phpfreaks.com/topic/48813-solved-bad-usage-of-class-keyword/ Share on other sites More sharing options...
roopurt18 Posted April 26, 2007 Author Share Posted April 26, 2007 Never mind. It's syntactically incorrect, at least as far as PHP4 is concerned. Quote Link to comment https://forums.phpfreaks.com/topic/48813-solved-bad-usage-of-class-keyword/#findComment-239245 Share on other sites More sharing options...
448191 Posted April 26, 2007 Share Posted April 26, 2007 It doesn't do much though, you're still fetching an object property since php4 doesn't support static properties, nor class constants. If you're not stuck with php4, I'd suggest using class constants instead (it is said they're faster than regular constants too - although I've never tested that). class Module { const CONST1 = 1; const CONST2 = 2; const CONST3 = 3; } Module::CONST1 You really only have three options: static properties, class constants, or properties. Any one might be the right choice on different occasions. Quote Link to comment https://forums.phpfreaks.com/topic/48813-solved-bad-usage-of-class-keyword/#findComment-239260 Share on other sites More sharing options...
roopurt18 Posted April 26, 2007 Author Share Posted April 26, 2007 It doesn't do much though, you're still fetching an object property since php4 doesn't support static properties, nor class constants. Exactly. I figured I'd go ahead and try it and as soon as I started to try and access one elsewhere I realized it wasn't going to happen. You can only access a class var through an instance in PHP4, which means I would have had to create an instance of the object to be accessed globally. So I guess the answer to my question is it leads to bad programming practice, at least in PHP4. I had other concerns, if it had even worked, about the "constants" not really being constants. And as far as typing is concerned, there's really not much difference between the two: $var = Module::CONST_VALUE; // Using mechanisms in PHP5 $var = MODULE_CONST_VALUE; // Using define() I just prefer the OOP aspect of the first method more, which I'm sure you agree with. Ultimately it was just a quick idea I had, figured I'd post before trying it out, and it ultimately turned into a scenario where it would have been better to remain silent and thought a fool than post and remove all doubt. Quote Link to comment https://forums.phpfreaks.com/topic/48813-solved-bad-usage-of-class-keyword/#findComment-239263 Share on other sites More sharing options...
448191 Posted April 26, 2007 Share Posted April 26, 2007 I just prefer the OOP aspect of the first method more, which I'm sure you agree with. I do, but it has practical advantages too, besides just being nice and OOPish. Apart from the assumed (I read it in a Matt Zandstra book) superior performance, it also prevents name clashes (indeed like namespaces). Ultimately it was just a quick idea I had, figured I'd post before trying it out, and it ultimately turned into a scenario where it would have been better to remain silent and thought a fool than post and remove all doubt. Even though php4 doesn't support class constants, you can live without them: function getConstant1(){ return 'somevalue'; } Module::getConstant1(); Overly simple, yes, but constants only allow scalar values anyway. I doubt it gets much faster either.. Quote Link to comment https://forums.phpfreaks.com/topic/48813-solved-bad-usage-of-class-keyword/#findComment-239268 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.