John Davidson

php - can I bind interface to my service and call method statically?

0 comments
Message:


There is already interface that I don`t want to change it or edit it.


<?php

namespace App\Contracts;

interface MyInterface
{
public function set(array $data): bool;
}

And I created a service.


<?php

namespace App\Services;

use App\Contracts\MyInterface;

class MyService implements MyInterface
{
protected $data;

public function set(array $data): bool
{
try {
$this->data = $data;
return true;

} catch (\Throwable $th) {
return false;
}
}
}

and the question is, is there any way that I can call set method statically without editing MyInterface? (for example in binding)


$service = app(MyInterface::class);
$service::set(["some-key"=> "some-value"]);


My Answer:
No, you cannot bind an interface to a service and call a method statically. Interfaces define a contract for classes to implement, and they do not have any implementation logic themselves. In order to call a method defined in an interface, you need to create a class that implements the interface and then call the method on an instance of that class. Static methods cannot be defined in interfaces, so you cannot call a method defined in an interface statically.

Rate this post

5 of 5 based on 3076 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us