2

I am trying to store all the attribute predicates within a static array in PHP:

public static $attributeValidation = [
    "attribute1" => function ($value) {
        return is_bool($value);
    },"attribute2" => function ($value) {
        return is_int($value);
    },
];

I am getting this following error:

expression is not allowed as default field value.

The goal is to simplify attribute validation by using

$attributeValidation["attribute"]($someValue);

Is there a way to accomplish this without using a local/private variables?

0

1 Answer 1

4

Is there a way to accomplish this without using a local/private variables

Yes. Simply create static method which would return the array you need:

public static function getAttributeValidation(): array
{
   return [
       // populate your array as you like
       ...
   ];
}

and then in your code instead of SomeClass::$attributeValidation you will do SomeClass::getAttributeValidation() to get that array.

Sign up to request clarification or add additional context in comments.

Comments

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.