Viewed   225 times

I am trying to create a constant name dynamically and then get at the value.

define( CONSTANT_1 , "Some value" ) ;

// try to use it dynamically ...
$constant_number = 1 ;
$constant_name = ("CONSTANT_" . $constant_number) ;

// try to assign the constant value to a variable...
$constant_value = $constant_name;

But I find that $constant value still contains the NAME of the constant, and not the VALUE.

I tried the second level of indirection as well $$constant_name But that would make it a variable not a constant.

Can somebody throw some light on this?



echo constant($constant_name);
Sunday, December 4, 2022

You should first concatenate $item->title onto set_lang() and place that in a variable.

You can then use that variable to call the right property on the obj.


$itemLangTitle = $item->title . set_lang();

echo $item->$itemLangTitle

You can use variables as properties dynamically but be careful with user input as always!

On a sidenote you also need to make sure the property exists otherwise you'll get errors. So make sure that if this doesn't yield anything it should fallback to english naming or something unobstructive ;)

Saturday, October 1, 2022

A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren't actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.

The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thusly: [a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*

From: Constants (PHP mamual)

Thursday, November 3, 2022

Define statements are in general only commented with a descriptive text, so that's basically how you comment it.

To read more about the DocBlock template tag, /**#@+, check out the manual page.

Monday, August 22, 2022

If you are using PHP 5.3.0 or greater, you can use the following:


Unfortunately, if you are using a version lower than 5.3.0, you are stuck using eval() (get_class_vars() will not work if the value is dynamic).

$value = eval($classname.'::$'.$propertyname.';');

EDIT: I've just said get_class_vars() wouldn't work if the value is dynamic, but apparently, variable static members are part of "the default properties of a class". You could use the following wrapper:

function get_user_prop($className, $property) {
  if(!class_exists($className)) return null;
  if(!property_exists($className, $property)) return null;

  $vars = get_class_vars($className);
  return $vars[$property];

class Foo { static $bar = 'Fizz'; }

echo get_user_prop('Foo', 'bar'); // echoes Fizz
Foo::$bar = 'Buzz';
echo get_user_prop('Foo', 'bar'); // echoes Buzz

Unfortunately, if you want to set the value of the variable, you will still need to use eval(), but with some validation in place, it's not so evil.

function set_user_prop($className, $property,$value) {
  if(!class_exists($className)) return false;
  if(!property_exists($className, $property)) return false;

  /* Since I cannot trust the value of $value
   * I am putting it in single quotes (I don't
   * want its value to be evaled. Now it will
   * just be parsed as a variable reference).
  return true;

class Foo { static $bar = 'Fizz'; }

echo get_user_prop('Foo', 'bar'); // echoes Fizz
set_user_prop('Foo', 'bar', 'Buzz');
echo get_user_prop('Foo', 'bar'); // echoes Buzz

set_user_prop() with this validation should be secure. If people start putting random things as $className and $property, it will exit out of the function as it won't be an existing class or property. As of $value, it is never actually parsed as code so whatever they put in there won't affect the script.

Thursday, August 25, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :