1

我正在研究一个 PHP 类,该类的必填字段之一是 ( DropoffType) 答案是REGULARPICKUP。我可以在课堂上写这个然后把“=”放到REGULARPICKUP?

例子:

protected $DropoffType = 'REGULARPICKUP';
4

1 回答 1

3

你当然可以

class MyClass
{
    protected $DropoffType = 'REGULARPICKUP';
}

或在构造函数中执行(这取决于您的具体情况是否可行):

class MyClass
{
    protected $DropoffType;

    public function __construct($Dropofftype = 'REGULARPICKUP')
    {
        $this->DropoffType = $Dropofftype;
    }
}

或者,如果您有一个 set 功能:

class MyClass
{
    protected $DropoffType;

    public function setDropoffType($Dropofftype = 'REGULARPICKUP')
    {
        $this->DropoffType = $Dropofftype;
    }
}
于 2012-01-15T04:04:52.080 回答