缺少__construct中的参数

时间:2012-11-28 13:40:58

标签: php phpmyadmin

我在运行时收到此错误:

$db = new PDO('mysql:host=localhost;dbname=MYDATABASE', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

$Manager = new CompanyManager($db);
$Manager->getList();

错误:

Warning: Missing argument 2 for Company::__construct(), called in /.../CompanyManager.class.php on line 53 and defined in /.../Company.class.php on line 17

以下是CompanyManager.class.php的一部分:

  public function getList()
  {
    $Company = array();

    $q = $this->_db->query('SELECT id, statut, company, activity, source, secteur, comments, offer_date, entry_date, type_ope, gestionnaire, next_step FROM prospect ORDER BY id');

    while ($donnees = $q->fetch(PDO::FETCH_ASSOC))
    {
      $Company[] = new Company($donnees);
    }

    return $Company;
  }

这是Company.class.php(有趣的部分):

<?php
class Company
{
    private $_id;
    private $_statut;
    private $_company;
    private $_activity;
    private $_source;
    private $_secteur;
    private $_comments;
    private $_offerDate;
    private $_entryDate;
    private $_typeOpe;
    private $_gestionnaire;
    private $_nextStep;

    public function __construct($id, $statut, $company, $activity, $source, $secteur, $comments, $offerDate, $entryDate, $typeOpe, $gestionnaire, $nextStep)
    {
        $this->setId($id);
        $this->setStatut($statut);
        $this->setCompany($company);
        $this->setActivity($activity);
        $this->setSource($source);
        $this->setSecteur($secteur);
        $this->setComments($comments);
        $this->setOfferDate($offerDate);
        $this->setEntryDate($entryDate);
        $this->setTypeOpe($typeOpe);
        $this->setGestionnaire($gestionnaire);
        $this->setNextStep($nextStep);
    }

非常感谢所有想要帮助我的人; - )

3 个答案:

答案 0 :(得分:2)

public function getList()
  {
    $Company = array();

    $q = $this->_db->query('SELECT id, statut, company, activity, source, secteur, comments, offer_date, entry_date, type_ope, gestionnaire, next_step FROM prospect ORDER BY id');

    while ($donnees = $q->fetch(PDO::FETCH_ASSOC))
    {
      $Company[] = new Company($donnees['id'], $donnees['statut'], $donnees['company'], $donnees['activity'], $donnees['source'], .....);
    }

    return $Company;
  }

否则你可以这样做

    public function __construct(array $data) {
       $this->setId($data['id']);
       .
       .
       .
       .
    }

答案 1 :(得分:1)

您的Company类需要传递__construct()参数列表中的所有参数。它们都不是可选的。

看起来您正在将数组传递给您的公司类,您需要根据参数列表传递每个值。

答案 2 :(得分:1)

您必须隐式地将变量传递给公司的构造函数:

public function getList()
  {
    $Company = array();

    $q = $this->_db->query('SELECT id, statut, company, activity, source, secteur, comments, offer_date, entry_date, type_ope, gestionnaire, next_step FROM prospect ORDER BY id');

    while (list($id, $status, $company, /*etc*/) = $q->fetch(PDO::FETCH_ASSOC))
    {
      $Company[] = new Company($id, $status, $company, /*etc*/);
    }

    return $Company;
  }