绑定提供程序laravel中的抽象类服务

绑定提供程序laravel中的抽象类服务,laravel,interface,abstract-class,service-provider,Laravel,Interface,Abstract Class,Service Provider,给出一个错误: 目标[MyVendor\ProductList\ProductServiceInterface]不可实例化 ProductServiceInterface namespace MyVendor\ProductList; interface ProductServiceInterface { public function productList(); public function getProductSpeed(); } namespace MyVendor

给出一个错误:

目标[MyVendor\ProductList\ProductServiceInterface]不可实例化

ProductServiceInterface

namespace MyVendor\ProductList;

interface ProductServiceInterface
{
    public function productList();
    public function getProductSpeed();
}
namespace MyVendor\ProductList\Service;

abstract class ProductColorService implements \MyVendor\ProductList\ProductServiceInterface
{
    public function productList()
    {
        $color = "black";
        return $color;
    }
}
ProductColorService

namespace MyVendor\ProductList;

interface ProductServiceInterface
{
    public function productList();
    public function getProductSpeed();
}
namespace MyVendor\ProductList\Service;

abstract class ProductColorService implements \MyVendor\ProductList\ProductServiceInterface
{
    public function productList()
    {
        $color = "black";
        return $color;
    }
}
**ProductSpeedService**

namespace MyVendor\ProductList\Service;

abstract class ProductSpeedService implements \MyVendor\ProductList\ProductServiceInterface {

    public function getProductSpeed() {
        $speed = 200;
        return $speed;
    }


}
提供商

namespace MyVendor\ProductList;

use Illuminate\Support\ServiceProvider;
use MyVendor\ProductList\ProductServiceInterface;
use MyVendor\ProductList\Service\ProductColorService;

class ProductColorServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->loadRoutesFrom(__DIR__ . '/routes/color.php');
    }

    public function register()
    {
        $this->app->bind('MyVendor\ProductList\ProductServiceInterface','MyVendor\ProductList\Service\ProductColorService');

        $this->app->bind('MyVendor\ProductList\ProductServiceInterface','MyVendor\ProductList\Service\ProductSpeedService');
    }
}

这个代码有什么问题?实际上,我需要使用服务重写接口中的一些函数。ie每个服务应该覆盖一个方法

namespace MyVendor\ProductList\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use MyVendor\ProductList\Models\Product;
use MyVendor\ProductList\ProductServiceInterface;

class ProductListController extends Controller
{
    public function index(ProductServiceInterface $product_service)
    {
        $color = $product_service->productList();
        $speed = $product_service->getProductSpeed();
        dd($color," = ",$speed);
        $product = Product::get();
        return view('ProductList::product',compact('product'));
    }
}

在这里,一个服务中的
productList()
和另一个服务中的
productSpeed()
不能实例化抽象类。它们的全部要点是永远不可能有它们的实例,因此,如果您使用方法将接口绑定到抽象类,它就永远不可能是抽象的。摘要用于继承结构中的类,您不希望用户创建新的类

所以没有理由抽象,抽象是为了这样的东西

abstract class Vehicle {
    protected $wheels;

    public function getWheels() {
        return $this->wheels;
    }
}

class Car extends Vehicle {
    protected $wheels = 4;
}

class Bike extends Vehicle {
    protected $wheels = 2;
}

您不能创建一个工具,因为它不是一个具体的实现,因为它是一个父类,用于帮助跨两个类共享方法。所以简短的版本,只需删除抽象关键字。如果您不知道为什么要添加它,则不需要它。

您没有实现
getProductSpeed
btw,。。对于该接口,您只有一个绑定,您需要更多地解释您正在尝试执行的操作,因为在此上下文中,ProductServiceInterface只能解析为一个实现ProductServiceInterface的具体操作,而ProductColorService是抽象的,所以它不能是具体的(不能是接口的绑定,因为无法实例化)感谢您的回复,很抱歉包含productSpeedService我刚刚编辑了这个问题,我需要从每个服务中添加一些条件,并通过接口从控制器获取所有这些条件这不是工作原理…
$product\u service
是实现接口的类的单个对象…多个绑定对于一个接口将相互重写,您只能使用上下文绑定来实现,这在这里没有帮助,即使这样,您也不会得到不同的类,除了ProductColorService是抽象的这一事实之外,我无法理解您,我只需要从一个服务重写一个函数,因此我将从每个服务重写一个函数只有一个函数和一个控制器,我想要所有的值,不使用它给出的抽象错误。我在一个接口中有多个方法,并从单独的服务中重写每个方法,即一个服务对应一个方法()在接口中,那么我如何实现它呢?为什么类必须是抽象的?抽象意味着你不能实例化它