Hi.
This can be discussed if it is right or wrong.
But the PHP manual states this about class constants:
The value must be a constant expression, not (for example) a variable, a class member, result of a mathematical operation or a function call.
Now what is a constant expression?
Take this code.
class myFirstConstants
{
const PHPed = 'IS COOL';
}
class mySecondConstants
{
const PHPExpress = myFirstConstants::PHPed;
}
echo "Nusphere ";
echo mySecondConstants::PHPExpress; |
If you run this code you get no errors but get the result
Nusphere IS COOL
So using a class constant from another class to declare the value of your own class constants seams to be legal in PHP.
But PHPed can't do codecompletion on the constant declaration line in the mySecondConstants. It simply does not work pressing CTRL space after you have written
myFirstConstants::
Why would you want to create your own version of a perfectly valid constant.
Well the reason is that I am building a framework that uses PDO to access and manipulate data, and PDO does not have types build in for some of the most common data types.
It only supports:
PDO::PARAM_BOOL - for Boolean fields (which basicaly is an integer)
PDO::PARAM_NULL - For SQL Null
PDO::PARAM_INT - For integers
PDO::PARAM_STR - For strings and VARCHAR etc.
PDO::PARAM_LOB - FOR BLOB (LOB)
PDO::PARAM_STMT - An at the moment not supported field type by any database.
But PDO does not cover the field column type float.
So that is why I want to use the existing constants, so I can cover any future field types added to PDO by the PHP development team.
class myFieldTypes
{
const mySTRING = PDO::PARAM_STR;
const myINT = PDO::PARAM_INT;
const myFLOAT = -1;
} |
And then in the code I can write this
function bindParam($fieldname, @$bindVariable, $fieldType)
{
$placeholder = ':' . $fieldname;
if ($fieldType > 0)
$statement->bindParam($placeHolder, @$bindVariable, $fieldType);
else
$statement->bindParam($placeHolder, @$bindVariable);
} |
And when PDO gets updated to include the Float column type, all I have to alter is the constant in myFieldTypes
Hope you see the picture.
A. As long as it is a constant expression it is valid to create a class constant with another constant value
B. Code completion does not work if you do that.
Pheww long forum post