duansha6410 2017-04-18 21:39
浏览 22
已采纳

类连接器中PDO的实例

I'm working on a project today and i've been working for a while now and i don't see what i do wrong here. Can someone give me the right example.

Thnx a lot!

Connector:

class Repository
{
private $connector;

public function __construct(Config $connector)
{
    $this->connector = $connector;
}

public function events()
{
    $query = 'SELECT * FROM digi_gz_parties';
    $dbh_query = $this->connector->getDatabase()->prepare($query);
    $dbh_query->execute();
    $dbh_querys = $dbh_query->fetchAll();

    return $dbh_querys;
}
}

Getter:

class REST
{
public function getEvents()
{
    require 'logic/Repository.php';

    $event = new Repository();
    $events = $event->events();

    return $events;
}
}

Error: Argument 1 passed to Repository::__construct() must be an instance of Config.

I know i need to give a paramater to the repository but i don't want it, i want only to call the repository without give some paramter.

Thanks a lot!

  • 写回答

1条回答 默认 最新

  • dsgw8802 2017-04-18 22:22
    关注

    You can change your Repository::__construct() to have a default $connector to null:

    public function __construct(Config $connector = null)
    {
        $this->connector = $connector;
    }
    

    That way, if you instanciate your object without any parameter, like you do here, it will default to null. The only downside of that is that, now, you have to be extra careful when using $this->connector inside your Repository class and remember it could be null.

    For exemple, here, the second line of your events() method is not going to work, because you lack the proper configuration to connect to your database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)