Not a bug Extra data constant - boleans error

cclaerhout

Well-known member
First of all, I'm not sure it's a bug or if it's the expected behaviour.

In the datawriter when we want to set a constant to be used after as an "extra data" it can't be anymore a boolean. If it is, when we use the function "getExtraData" the php function array_key_exists of the xenforo datawriter function "isExtraDataSet" is not going to like it:
The first argument should be either a string or an integer

Can it be changed or do I have to modify the way I'm using the constant?
 
Am I understanding this right, that you're effectively doing this?
Code:
$extraDataKey = true;
$dw->getExtraData($extraDataKey);
That doesn't really make sense to me - it's a key so by definition it needs to be a string or an integer (probably a string).
 
May be I don't use it correctly, but I've just tried to use a pattern I've found a few month ago on XenForo files but with a bolean and not a string.

Example:

> Extended Datawriter:
PHP:
 const ALLOW_OVERRIDE = false;

> In the same Extended Datawriter but in a function this time:
PHP:
$canOverride = $this->getExtraData(self::ALLOW_OVERRIDE);

> And in my controller (admin):
PHP:
$dw->setExtraData(myClass::ALLOW_OVERRIDE, true);
 
Maybe you could use an option in this case. In your datawriter:
PHP:
const OPTION_ALLOW_OVERRIDE = 'allowOverride';

    protected function _getDefaultOptions()
    {
        return array(
            self::OPTION_ALLOW_OVERRIDE => false,
        );
    }

PHP:
$canOverride = $this->getOption(self::OPTION_ALLOW_OVERRIDE);

PHP:
$dw->setOption(myClass::OPTION_ALLOW_OVERRIDE, true);
 
Just for reference, when you define this:
Code:
const ALLOW_OVERRIDE = false;
You're not defining a name value pair. You're defining self::ALLOW_OVERRIDE / ClassName::ALLOW_OVERRIDE to be false. It's really just an unchangeable variable.

In this case, the define is used as a key (rather than a value), so that doesn't really make sense.

It's worth noting that the only reason we use defines is it creates a form of documentation as to the potential extra data/options available to a datawriter.
 
Top Bottom