我有这个类
class user{
public function getmsg(){
$sql3 = "SELECT * FROM news WHERE usertype = 'admin'";
$result = mysqli_query($this->db,$sql3);
while($user_data = mysqli_fetch_array($result)){
$whoposted = $user_data['whoposted'];
$dateposted = $user_data['dateposted'];
$title = $user_data['title'];
$content = $user_data['content'];
$whoposted = $user_data['usertype'];
}
}
}
假设对象"db"已被声明。 我进入 getmsg 函数的方式是
$user = new user();
$user->getmsg();
现在我想获取$dateposted的内容,我该怎么做?
任何帮助将不胜感激。谢谢。
你真的需要阅读变量作用域,特别是:
默认情况下,函数内部使用的任何变量都仅限于局部函数范围。
换句话说,当getmsg()
函数退出时,您的变量$whoposted
等将被销毁。
更不用说在函数中,它们的值在循环的每次迭代中都会被覆盖。
您可能希望在存储数据的对象中定义属性。 但是,对于user
对象来说,检索与多个用户关联的新闻文章确实是一件奇怪的事情。 也许更明智的设计是让一个usertype
对象代表管理员用户;并从那里实例化(从数据库查询的结果)单独的news
对象来表示每篇文章?
-
定义一个类来表示您的新闻文章:
class NewsArticle { const FORMAT_DATETIME_MYSQL = 'Y-m-d H:i:s'; protected $author; protected $date; protected $title; protected $content; function __construct($arr) { $this->author = $arr['whoposted']; // would probably actually pass this // to a getUser() function that returns // a User object $this->date = DateTime::createFromFormat( self::FORMAT_DATETIME_MYSQL, $arr['dateposted'] ); $this->title = $arr['title']; $this->content = $arr['content']; } function getAuthor() {return $this->author;} function getDate() {return strftime('%c', $this->date->getTimestamp());} function getTitle() {return $this->title;} function getContent() {return $this->content;} }
-
定义一个类来表示您的"用户类型":
class UserType { const SQL_GET_NEWS = 'SELECT * FROM news WHERE usertype = ?'; protected $type; function __construct($type) { $this->type = $type; } function getNewsArticles() { global $mysqli; if ($stmt = $mysqli->prepare($db, self::SQL_GET_NEWS)) { $stmt->bind_param('s', $this->type); $stmt->execute(); $result = $stmt->get_result(); $articles = array(); while ($row = $result->fetch_assoc()) $articles[] = new NewsArticle($row); $stmt->close(); return $articles; } return null; } }
-
然后,您可以执行以下操作:
$adminType = new UserType('admin'); foreach ($adminType->getNewsArticles() as $article) { echo '<h2>', $article->getTitle(), '</h2>'; echo '<p>By ', $article->getAuthor(), ' at ', $article->getDate(), '</p>'; echo $article->getContent(); }
综上所述,其中大部分只是重新发明轮子来构建一个(非常简单的)自定义ORM。 已经有很多PHP ORM解决方案,其中许多是免费的和开源的。
您的问题是,您必须返回完整的消息数组才能访问所需的数据,如下所示(您的类有一些改进):
class user
{
private $msg = null;
private function loadData(){
$sql3 = "SELECT * FROM news WHERE usertype = 'admin'";
$result = mysqli_query($this->db, $sql3);
$this->msg = mysqli_fetch_assoc($result);
}
public function getmsg()
{
if(null === $this->msg){
$this->loadData();
}
return $this->msg;
}
}
$user = new user();
// since PHP 5.4
echo $user->getmsg()['dateposted'];
// for older PHP versions you can use this
$msg = $user->getmsg();
echo $msg['dateposted'];
或者,您可以使用消息对象。但解决方案是将数据保存在某个地方和/或返回完整结果。
请尝试此过程
<?php
class user{
public function getmsg($key){
$result=array();
$sql3 = "SELECT * FROM news WHERE usertype = 'admin'";
$result = mysqli_query($this->db,$sql3);
$user_data = mysqli_fetch_array($result);
return $user_data[$key];
}
}
$user=new user();
echo $user->getmsg('dateposted');