我想从类数据库传递一个数组到类的字符,有人可以帮助我吗?如何在OOP PHP中传递数组?
我的代码:
级数据库
class Database {
public function DBCharactersMerchandise() {
$db = $this->connection();
$uid = $_SESSION['id'];
$this->conn = $db->prepare("SELECT * FROM mercadoria WHERE cid = :mid");
$this->conn->execute(array(':mid' => $uid));
while($row = $this->conn->fetch(PDO::FETCH_ASSOC)){
$array = [
$row['type'], // here I have 'apple' and 'orange'
];
return $array;
}
}
}
阶级属性
Class Characters {
public function getUserMerchandise() {
$db = new Database();
$type = $db->DBCharactersMerchandise();
foreach ($type as $value) {
echo $value; // I want echo 'apple' and 'orange' here
}
}
}
我想你能明白我想要什么,所以如果有人可以帮助我我很感激。
我发现,你做'$返回数组中可能出现的问题;'在类'Database' –
你可以尝试寻找到[依赖注入(http://krasimirtsonev.com/blog /条/依赖注入功能于PHP,例如,如何对DI-创建你自己的依赖注入容器)。这听起来很适合你的用例。 –
你只需要'type'字段值吗? – RomanPerekhrest