未捕获的错误:无法实例化接口,其中接口已在类中使用

问题描述 投票:0回答:0

我有一个名为 ProductUpdateInterface 的接口,代码如下

<?php
namespace AmdNetsuitePQ\NetsuitePQ\Api;

interface ProductUpdateInterface
{
    /**
     * Update product quantity and price
     *
     * @param int $productId
     * @param float|null $price
     * @param float|null $quantity
     * @return bool
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     * @throws \Magento\Framework\Exception\InputException
     * @throws \Magento\Framework\Exception\StateException
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function update($productId, $price = null, $quantity = null);
}

还有 ProductUpdate 类。请看下面的代码

<?php
namespace AmdNetsuitePQ\NetsuitePQ\Model;

use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ProductFactory;
use AmdNetsuitePQ\NetsuitePQ\Api\ProductUpdateInterface;

class ProductUpdate implements ProductUpdateInterface
{
    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;

    public function __construct(
        ProductRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function update($productId, $price = null, $quantity = null)
    {
        $productId = (int) $productId;
        $product = $this->productRepository->getById($productId);
        if (!$product->getId()) {
            throw new NoSuchEntityException(__('Product with ID %1 not found.', $productId));
        }
        if ($price !== null && $price < 0) {
            throw new InputException(__('Price must be greater than or equal to 0.'));
        }
        if ($quantity !== null && $quantity < 0) {
            throw new InputException(__('Quantity must be greater than or equal to 0.'));
        }
        if ($price !== null) {
            $product->setPrice($price);
        }
        if ($quantity !== null) {
            $product->setQty($quantity);
            $product->setIsQtyDecimal(false);
        }
        $this->productRepository->save($product);
        
        return $productId;
    }
}

现在当我执行这些时(实际上这些是 Magento2 模块中的代码)我得到以下错误

致命错误:未捕获错误:无法实例化接口 AmdNetsuitePQ\NetsuitePQ\Api\ProductUpdateInterface

请帮忙解决这个错误

php oop interface magento2
© www.soinside.com 2019 - 2024. All rights reserved.