【发布时间】:2015-03-25 04:18:49
【问题描述】:
我试图将一个类嵌套在 PHP 中的另一个类中,并通过 HTML 显示数据。我有一个问题类,在该类中,我创建了一个用户对象。用户对象包含创建问题的用户的信息。出于某种原因,我在输出中收到“(!)致命错误:无法访问私有财产问题::$ user”。我能说的没有什么是私人的,这会阻止我提取数据。下面是一段代码:
class Question extends Model {
private $user;
private $content;
private $title;
private $tags;
private $date;
public function __construct($user, $content, $title, $tags, $date) {
parent::__construct();
$this -> setUser($user);
$this -> setContent($content);
$this -> setTitle($title);
$this -> setTags($tags);
$this -> setDate($date);
}
public function getUser() {
return $this -> user;
}
public function setUser($user) {
$this -> user = User::findUser($user);
return $this;
}
那么这是我的用户类:
class User extends Model {
private $email;
private $password;
private $fname;
private $lname;
public function __construct($email, $password, $fname, $lname) {
parent::__construct();
$this -> setEmail($email);
$this -> setPassword($password);
$this -> setFname($fname);
$this -> setLname($lname);
}
创建问题和用户对象的函数如下:
private static function makeQuestionFromRow($row) {
$question = new Question($row['user_id'], $row['quest_Content'], $row['quest_Title'], $row['quest_Tags'], $row['quest_DatePosted']);
$question -> id = $row['quest_ID'];
return $question;
}
private static function makeUserFromRow($row) {
$user = new User($row['user_email'], $row['user_pwd'], $row['user_fname'], $row['user_lname']);
$user -> id = $row['user_id'];
return $user;
}
两者都是模型的子类:
class Model {
protected $id;
public function __construct() {
}
public function getId() {
return $this -> id;
}
我正在尝试显示用户的名字和姓氏,并使其成为用户页面的链接,该页面查看用户信息。所以我的代码是:
<b>Date Posted: </b>{{date("m/d/Y", strtotime($question->getDate()))}} by <a href="@@user/view/{{$question->user->getId()}}@@"> TEST</a> <br /><br />
日期代码正确,但用户 ID 没有出现并链接到上面的“测试”。我不确定我是否在做一些语法错误或什么。如果有人可以帮助我,我将不胜感激。
这是作业。不试图克服任何人。再次感谢。
【问题讨论】:
-
链接的@@ 是一个通配符,它允许我使用相对链接,括号被替换为