Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
I have a class called UCA_Member (which is a model within MVC) with two constants, STANDARD and PREFERRED... now I need to select an icon depending upon whether a value (from the database) is STANDARD or PREFERRED. Some day I may add another constant... something like SUBSTANDARD. What would be the best way to do that without making MORE work for myself in the futer ie: having to create a directory as well as adding the constant. Thanks!
The class looks like this (extremely simplified for your sake)
<?php
class UCA_Member
{
protected $_table = "members";
const STANDARD = 1;
const PREFERRED = 2;
public function getIcon()
{
if($this->type == self::STANDARD)
{
// Not sure what to do
}
}
}
Maybe it would make more sense to do something like this?
Although I really like this method because it allows me to do that, it seems like it's going to cause more work for me because now if I want to add another type, I have to go through and find all the places where the types are referenced and add another type:
public function getIcon()
{
switch($this->type)
{
case self::PREFERRED;
$icon = 'preferred';
break;
case self::SUBSTANDARD;
$icon = 'substandard';
break;
case self::STANDARD:
default:
$icon = 'standard';
break;
}
return $icon;
}
is identical to the snippet I posted, sans the lower case, which can easily be fixed either by having your original values all lower case, or passing your return through strtolower()
I don't think using constants breaks encapsulation. Why do we encapsulate at all? Mainly to avoid one bit of code meddling with another. You can't set a constant so I don't see what the problem is.
Oh Ninja how about you use a string? What do these constants mean anyway?
OK, so let's say I go with this method... (yes, corridors is new--I guess it's relevant so I should include it)
In my table, I have a column called type and I have a column called corridor... I was hoping to just store a 4 digit (maximum) integer in each of these columns... then use the class to grab the icon based on what corridor and what type. The corridor determines the color, and the type determines the icon. I would prefer the colors and icons to be defined in the class... so with all that in mind, what do you guys think of this method?
You can do that just fine. Accessing static data is globally fine. Accessing an object's data from a static member will not work without injecting this object into the function.
I use class constants for things which don't change. Typically I'll use them for configuration options:
abstract class Swift_Log_Base implements Swift_Log
{
/**
* A command type entry
*/
const COMMAND = ">>";
/**
* A response type entry
*/
const RESPONSE = "<<";
/**
* An error type entry
*/
const ERROR = "!!";
/**
* A standard entry
*/
const NORMAL = "++";
// ... snipp ....
/**
* Add a new entry to the log
* @param string The information to log
* @param string The type of entry (see the constants: COMMAND, RESPONSE, ERROR, NORMAL)
*/
abstract public function add($text, $label);
// ... snip ...
}
I don't see the harm in using them provided you're happy that you won't want to change the values dynamically.