我是面向对象编程的新手,我无法找到为什么我的这个练习项目是错误的。它不断给我提供以下错误信息:
“警告:缺少Character :: __ construct()的参数2,在第26行的C:\ Users \ User \ Documents \ NetBeansProjects \ PhpProject3 \ poo_bdd \ connect_bdd.php中调用,并在C:\ Users \ User \中定义第13行的Documents \ NetBeansProjects \ 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;
}
}
&GT;
和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());
}
&GT;
SQL tbale有6列:id,name,strength,damage,level,experience。
如果有人有线索,那就太好了。
感谢您的帮助!
答案 0 :(得分: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"]);
}
答案 1 :(得分:1)
警告显示为您的构造函数需要6个参数,但您只给他1:
$character = new Character($data);
如果您不想发出警告,您必须将所有6个参数(至少使用'')传递给构造函数,或者您必须使用非必需参数的默认值。
答案 2 :(得分:1)
类Character的构造函数定义为:
__construct($id, $name,$strength,$damage,$level,$experience)
但你称之为:
$character = new Character($data);
哪个看起来更像你的水合物功能?
答案 3 :(得分:0)
试试这个:
$character = new Character($data['id'],$data['name'],$data['strength'],$data['damage'],$data['level'],$data['experience']);
答案 4 :(得分:0)
在实例化循环中,使用extract
- 函数将数据提取到当前范围内的变量中:
//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();
}