barrywood Posted November 2, 2009 Share Posted November 2, 2009 In my old non-OOP PHP coding I used defines and string arrays to create constants and string representations of them like this: define ('_MYAPP_PAYSTAT_WAITING', 1); define ('_MYAPP_PAYSTAT_PAID', 2); $myapp_paystat_text[_MYAPP_PAYSTAT_WAITING] = 'waiting for payment'; $myapp_paystat_text[_MYAPP_PAYSTAT_PAID] = 'paid in full'; The downside is that I have to make sure I declare $myapp_paystat_text as a global variable in any functions that need to have access to the text representation. I'd like to put this info into a class instead. Here's what I've come up with so far: class MYAPP { const PAYSTAT_WAITING = 1; const PAYSTAT_PAID = 2; function PAYSTAT($dispCode) { switch ($dispCode) { case self:: PAYSTAT_WAITING: return ('Waiting'); case self:: PAYSTAT_PAID: return ('Paid'); } } } Using this class I would access the constants like this: if ($status == MYAPP::PAYSTAT_WAITING) echo "Your order status is currently ". MYAPP::PAYSTAT($status); Does this seem like a reasonable way to approach this or is there a cleaner way? Quote Link to comment https://forums.phpfreaks.com/topic/179996-solved-class-constants/ Share on other sites More sharing options...
philoertel Posted November 2, 2009 Share Posted November 2, 2009 That's a good change. A note of caution as you start doing things the OO way: avoid classes with names like MYAPP that store all your application's data and behavior. OOP works best with lots of discrete little objects. Put these constants in a class called something like "Payment". Put that $status variable in there too, along with functions like getStatusText(). You'll have other objects like Order, Customer, and Product, and you'll have a devil of a time figuring out what code and what data goes where, but you'll end up with an OO application that's easier to understand and maintain. Quote Link to comment https://forums.phpfreaks.com/topic/179996-solved-class-constants/#findComment-949584 Share on other sites More sharing options...
mikesta707 Posted November 2, 2009 Share Posted November 2, 2009 btw if you want to access the function like that in the class, you need to make the method static Quote Link to comment https://forums.phpfreaks.com/topic/179996-solved-class-constants/#findComment-949586 Share on other sites More sharing options...
barrywood Posted November 2, 2009 Author Share Posted November 2, 2009 I used MYAPP just for example purposes, I'll be using more meaningful and unique names in practice. Thanks for the suggestion to a different class for each group of data items. Quote Link to comment https://forums.phpfreaks.com/topic/179996-solved-class-constants/#findComment-949590 Share on other sites More sharing options...
barrywood Posted November 2, 2009 Author Share Posted November 2, 2009 @mikesta707: It appears that the functions are working fine as non-static functions without instantiating the class. It also works declaring them "static function" and as "public static function". I would think that public static would be the most accurate way to declare them so I'll be going with that. Thanks for the input. Quote Link to comment https://forums.phpfreaks.com/topic/179996-solved-class-constants/#findComment-949600 Share on other sites More sharing options...
mikesta707 Posted November 2, 2009 Share Posted November 2, 2009 ah sorry. Yeah I meant should not have to. Generally variables and methods that are accessed without instantiating a class are set as static. at least in my experience Quote Link to comment https://forums.phpfreaks.com/topic/179996-solved-class-constants/#findComment-949608 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.