0

I have this PHP code block -:

$visible_to = 'private';
$icon = array('public' => 'ICON_GLOBE', 'private' => 'ICON_LOCK');
echo TbHtml::$icon[$visible_to];

But running this prints -: Access to undeclared static property: TbHtml::$icon.

Whats the problem with my code? Thanks :)

Heres the TbHtml class -:

class TbHtml
{
     const ICON_GLOBE = 'Its a globe';
     const ICON_LOCK = 'Its a lock';
}
4
  • You need to show us the class TbHtml in order for us to figure that out. Commented Jan 14, 2014 at 18:08
  • 1
    The problem with your code is that you are trying to access an undeclared static property. Commented Jan 14, 2014 at 18:09
  • So what code change I need to make? Commented Jan 14, 2014 at 18:13
  • I want my code to get the value of $icon['public'] & not take it as a simple string. Commented Jan 14, 2014 at 18:15

3 Answers 3

2

People are down-voting Mubo, but his answer is closest to being correct in my opinion. You shouldn't be trying to call class constants using dynamic parts. Why not just do:

$visible_to = 'private';
$icon = array('public' => TbHtml::ICON_GLOBE, 'private' => TbHtml::ICON_LOCK);
echo $icon[$visible_to];
Sign up to request clarification or add additional context in comments.

1 Comment

A very simple and effective approach. The only downside to this might be that the calling code is stilled tightly coupled to the class as it needs to understand that static constant names. If you want to decouple this, you might consider exposing a static method that accepts "visibility" value and returns the proper static property value.
0

You can utilize get_class_vars() to get to this information:

$class_vars = get_class_vars('TbHtml');
$desired_value = $class_vars[$icon[$visible_to]];

I would however suggest exposing a static method in your class if you class needs to understand public vs. private:

public static function get_icon_by_visibility($visibility = 'public') {
    if ($visibility === 'public') {
        return self::ICON_GLOBE;
    } else if ($visibility === 'private') {
        return self::ICON_LOCK;
    } else {
        throw new Exception('Invalid paramater passed.');
    }
}

// usage
echo TbHtml::get_icon_by_visibility($visible_to); 

Comments

0

You need to do this:

echo TbHtml::${$icon[$visible_to]};

Two dollar signs so that you get the value of $icon[$visible_to] and use that as the variable name.

However, such trickery is bad code. It would be better practice if you did something like this, where you store the icons in an array so that you don't save the variable NAMES in the $icon array, but rather their KEYS:

class TbHtml
{
     const ICONS = array('GLOBE'=>'Its a globe', 'LOCK'=>'Its a lock')
}

and

$visible_to = 'private';
$icon = array('public' => 'GLOBE', 'private' => 'LOCK');
$visible_to_icon = $icon[$visible_to];
echo TbHtml::$globe[$visible_to_icon];

4 Comments

This answer may be technically correct, but variable variables ($$) are very poor practice and should be discouraged
So just do ${$icon[$visible_to]}? @JasonFingar
Doing TbHtml::$$icon[$visible_to]; results in Access to undeclared static property: TbHtml::$ICON_GLOBE. Your second option may be correct. But isn't there any way out which doesn't requires a change in the TbHtml class.
Must be using an odd order of operations, use the comment above, I'll update. Although, I just tested it and it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.