5

我正在关注这个包装

我有这个错误:Catchable fatal error: Argument 1 passed to XeroPHP\Models\Accounting\Invoice::setDueDate() must implement interface DateTimeInterface, string given

这是我的代码:

try{
            $lineitem = new LineItem($this->_xi);
            $lineitem->setAccountCode('200')
            ->setQuantity('5.400')
            ->setDescription('this is awesome test')
            ->setUnitAmount('9900.00');

            $contact = new Contact($this->_xi);
            $contact->setName("John Doe")
                ->setFirstName("John")
                ->setLastName("Doe")
                ->setEmailAddress("johngwapo@hot.com")
                ->setContactStatus(Contact::CONTACT_STATUS_ACTIVE);



            $invoice = new Invoice($this->_xi);
            $invoice->setType(Invoice::INVOICE_TYPE_ACCREC)
                ->setStatus(Invoice::INVOICE_STATUS_AUTHORISED)
                ->setContact($contact)
                //->setDate(\DateTimeInterface::format("Y-m-d"))
                ->setDueDate("2018-09-09")
                ->setLineAmountType(Invoice::LINEAMOUNT_TYPE_EXCLUSIVE)
                ->addLineItem($lineitem)
                ->setInvoiceNumber('10')
                ->save();



        }catch ( Exception $e ){
            $GLOBALS['log']->fatal('[Xero-createContact]-' . $e->getMessage());
            echo $e->getMessage();

        }

当我尝试这样做时:

->setDueDate(\DateTimeInterface::format("Y-m-d"))

我得到了这个错误:致命错误:不能静态调用非静态方法 DateTimeInterface::format(),假设 $this 来自不兼容的上下文

这是我调用的 setDueDate 函数:

 /**
     * @param \DateTimeInterface $value
     * @return Invoice
     */
public function setDueDate(\DateTimeInterface $value)
    {
        $this->propertyUpdated('DueDate', $value);
        $this->_data['DueDate'] = $value;
        return $this;
    }

对于如何使用此 DateTimeInterface 以及如何使用它设置未来日期以及如何解决所有这些错误,我真的很迷茫。

4

1 回答 1

20

第一个错误说,该->setDueDate($date)方法需要一个实现DateTimeInterface的对象,但您只提供了一个字符串->setDueDate("2018-09-09")

第二个错误说,该format($format)方法不能被静态调用。它需要一个格式模式,并根据提供的模式将现有对象格式化为字符串。但是,您尝试静态调用它,提供日期字符串而不是格式模式 - 难怪它失败了。您需要从字符串createFromFormat($format, $date_string)创建DateTime对象的方法,而不是相反。

解决方案是创建一个实现 DateTimeInterface 的对象。例如DateTimeDateTimeImmutable (相同,但从未修改)。如果您稍后可以修改此值,我建议您使用 DateTime。

所以改变这一行:

->setDueDate("2018-09-09")

对此:

->setDueDate(\DateTime::createFromFormat('Y-m-d', "2018-09-09"))

它应该像一个魅力。

于 2016-09-09T07:00:46.567 回答