How TP5 uses cookie? in validation classes

if you add public $uid=Cookie::get ("uid") to the validation class like this, you will report an error. Why do you report an error? Can"t you use cookie like this?

<?php
namespace app\index\validate;

use think\Validate;
use think\facade\Cookie;

class User extends Validate
{
    public $uid=Cookie::get("uid");
    protected $rule =   [
        "uid"   => "number|between:1,120",
    ];
}

Mar.19,2021

Class attribute defaults can only use constants.
you can set $uid in the constructor.
but pay attention to the problem of overriding the parent constructor.

Menu