0

我是面向对象编程的新手,我无法找到为什么我的这个练习项目是错误的。它不断给我以下错误消息:

“警告:Character::__construct() 缺少参数 2,在第 26 行的 C:\Users\User\Documents\NetBeansProjects\PhpProject3\poo_bdd\connect_bdd.php 中调用并在 C:\Users\User\Documents\NetBeansProjects 中定义第 13 行的 \PhpProject3\poo_bdd\Character.class 和参数 3 到 6 相同

类的定义

<?php
class Character 
    {
        //attributs de la classe
        private $_id;
        private $_name;
        private $_strength;
        private $_damage;
        private $_level;
        private $_experience;

        //on définit le constructeur
        public function __construct($id, $name,$strength,$damage,$level,$experience) 
        {
            $this->setId($id);
            $this->setName($name);
            $this->setStrength($strength);
            $this->setDamage($damage);
            $this->setLevel($level);
            $this->setExperience($experience);
        }

        //hydratation 
        public function hydrate(array $data)
        {
            foreach ($data as $key => $value)
            {
                $method = 'set'.ucfirst($key);

                if (method_exists($this, $method))
                {
                    $this->$method($value);
                }
            }

            if(isset($data['id']))
            {
                $this->setId($data['id']);
            }
            if(isset($data['name']))
            {
                $this->setName($data['name']);
            }
            if(isset($data['strength']))
            {
                $this->setStrength($data['strength']);
            }
            if(isset($data['damage']))
            {
                $this->setDamage($data['damage']);
            }
            if(isset($data['level']))
            {
                $this->setLevel($data['level']);
            }
            if(isset($data['experience']))
            {
                $this->setExperience($data['experience']);
            }

        }

        //on définit les getters et setters
            //getters
        public function getId()
        {
            return $this->_id;
        }
        public function getName()
        {
            return $this->_name;
        }
        public function getStrength()
        {
            return $this->_strength;
        }
        public function getDamage()
        {
            return $this->_damage;
        }
        public function getLevel()
        {
            return $this->_level;
        }
        public function getExperience()
        {
            return $this->_experience;
        }

            // setters
        public function setId($id)
        {
              $this->_id = (int) $id;
        }

        public function setName($name)
        {
            if (is_string($name) && strlen($name) <= 30)
            {
                $this->_name = $name;
            }
        }

        public function setStrength($strength)
        {
            $this->_strength = $strength;
        }

        public function setDamage($damage)
        {
            $this->_damage = $damage;
        }

        public function setLevel($level)
        {
            $this->_level = $level;
        }

        public function setExperience($experience)
        {
            $this->_experience = $experience;
        }


     }

?>

和PHP代码:

<?php

require ('Character.class');

try
    {

            //connexion à la table blog_messages 
            $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
            $bdd = new PDO('mysql:host=localhost;dbname=test; charset=UTF-8', 'root', '', $pdo_options);
            $bdd->query('SET NAMES utf8');

            //sélection des 3 derniers billets
            $request = $bdd->query('SELECT id, name, strength, damage, level, experience FROM personnages');

            //affichage des billets
            while ($data = $request->fetch(PDO::FETCH_ASSOC))
                {
                    $character = new Character($data);

                    echo $character->getName(),' has a strength of',$character->getStrength();
                }
            //on termine le traitement de la requète
            $request->closeCursor();
   }
        catch (Exception $e)
            {
                die('Erreur: '.$e->getMessage());
            } 

?>

SQL tbale 有 6 列:id、name、strength、damage、level、experience。

如果有人有线索,那就太好了。

谢谢你的帮助!

4

5 回答 5

3

您的构造函数接受 6 个参数,而您在这里只传递一个:

$character = new Character($data);

如果您希望构造函数接受单个关联数组,请将其用作您的__construct方法:

    public function __construct($data) 
    {
        $this->setId($data["id"]);
        $this->setName($data["name"]);
        $this->setStrength($data["strength"]);
        $this->setDamage($data["damage"]);
        $this->setLevel($data["level"]);
        $this->setExperience($data["experience"]);
    }
于 2012-11-26T10:25:56.877 回答
1

当您的构造函数采用 6 个参数但您只给他 1 个参数时,将显示警告:

$character = new Character($data);

如果您不想收到警告,则必须将所有 6 个参数(至少使用 '')传递给构造函数,或者您必须对非强制参数使用默认值。

于 2012-11-26T10:27:11.733 回答
1

Character 类的构造函数定义为:

__construct($id, $name,$strength,$damage,$level,$experience)

然而你这样称呼它:

$character = new Character($data);

哪个看起来更像你的水合物功能?

于 2012-11-26T10:27:25.163 回答
0

尝试这个:

$character = new Character($data['id'],$data['name'],$data['strength'],$data['damage'],$data['level'],$data['experience']);
于 2012-11-26T10:31:26.043 回答
0

在您的实例化循环中,使用extract-function 将数据提取到当前范围内的变量中:

//affichage des billets
while ($data = $request->fetch(PDO::FETCH_ASSOC))
{
   extract($data, EXTR_PREFIX_ALL, 'chr_');
   $character = new Character(
       $chr_id, $chr_name, $chr_str, 
       $chr_damage, $chr_level, $chr_experience
   );
   echo $character->getName(),' has a strength of',$character->getStrength();
}

请注意,提取函数有点神奇并会创建变量,因此您需要知道$data外观如何才能了解 -$chr_变量的来源。
更好的选择可能是创建一个工厂,以封装和可视化提取的数据:

abstract class CharacterFactory
{
  /**
   * Creates a new Character-object from the $data provided
   * @param array $data Character data as an array
   * @return Character A Character object with its properties set from $data
   */
  public static function create (array $data)
  {
    $id = $data['id'];
    $name = $data['name'];
    $str = $data['str'];
    $damage = $data['damage'];
    $level = $data['level']
    $experience = $data['experience'];
    return new Character($id, $name, $str, $damage, $level, $experience);
  }
}

// In your loop:
//affichage des billets
while ($data = $request->fetch(PDO::FETCH_ASSOC))
{
   $character = CharacterFactory::create($data);
   echo $character->getName(),' has a strength of',$character->getStrength();
}
于 2012-11-26T10:36:16.917 回答