简体   繁体   中英

Pass function in static variable of class PHP

I have a class with static variable:

 class MyClass {
    protected static $rules = [
         'name' => 'required'
         'help' => 'required|in:' . implode(',', custom_helper_function());
    ];
 }

I want use custom helper function in this variable and use implode. How I can do it? Now I get error:

expression is not allowed as field default value

Thanks.

You cant use functions on variable definitions. If it was just a regular one i would suggest you to assign the value on the constructor, but being an static one then that's not possible/ makes no sense.

Then you just can't. Though you can achieve something similiar with an static function:

 class MyClass {

    public static function getRules() {
        $rules = [
         'name' => 'required',
         'help' => ( 'required|in:' . implode(',', custom_helper_function()) )
        ];
        return $rules;
    }
 }
 MyClass::getRules();

Also, don't forget to define the custom_helper_function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM