我正在开发一个简单的项目,并且使用 PHP 面向对象。基本上我有一个类,其中包含有关如何从 MySQL 数据库获取数据并将其显示在页面上的信息。这个类叫做admin.php
像这样:
<?php
class Admin{
private $db,$username,$password,$id,$group,$Datetime,$msg;
public function __construct()
{
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function getAdmin($name)
{
if(!empty($name))
{
$adm = $this->db->prepare("select * from admins where username=?");
$adm->bindParam(1,$name);
$adm->execute();
while($row = $adm->fetch())
{
$this->id = $row['id'];
$this->username = $row['username'];
$this->password = $row['password'];
$this->group = $row['group'];
$this->Datetime = $row['date_joined'];
$this->msg = $row['welcome_message'];
}
}
}
public function getID()
{
return $this->id;
}
public function getUsername()
{
return $this->username;
}
public function getPassword()
{
return $this->password;
}
public function getGroup()
{
return $this->group;
}
public function gtDate()
{
return $this->Datetime;
}
public function welcomeMessage()
{
return $this->msg;
}
}
?>
然后在另一个名为 dashboard.php
的页面上,我已经包含了这个类文件并进行了编码:
<?php if ($dataSet->welcomeMessage()== 0){
echo "
<li class='dropdown messages-menu'>
<!-- Menu toggle button -->
<a href='#' class='dropdown-toggle' data-toggle='dropdown'>
<i class='fa fa-envelope-o'></i>
<span class='label label-success'>1</span>
</a>
<ul class='dropdown-menu'>
<li class='header'>You have one new message</li>
<li>
<!-- inner menu: contains the messages -->
<ul class='menu'>
<li><!-- start message -->
<a href='message.php?msg=".$dataSet->msg();."'>
<div class='pull-left'>
<!-- User Image -->
<img src='dist/img/user2-160x160.jpg' class='img-circle' alt='User Image'>
</div>
<!-- Message title and timestamp -->
<h4>
Support Team
<small><i class='fa fa-clock-o'></i>Just now</small>
</h4>
<!-- The message -->
<p>Welcome to your admin panel</p>
</a>
</li>
<!-- end message -->
</ul>
<!-- /.menu -->
</li>";
}
?>
但是每当我运行它时,我都会收到此错误消息:
解析错误:语法错误,意外的“.” dasboard.php 第 15 行
这是 dasboard 文件的第 15 行:
<a href='message.php?msg=".$dataSet->msg();."'>
非常确定,这一定与我在回显语句时在 html 标记内错误地串联和组合类方法有关。
那么正确的方法是什么?
最佳答案
“$dataSet->msg”不是一个函数,而是一个属性,你需要这样调用它。
<a href='message.php?msg=".$dataSet->msg."'> // but wont work as it is private
或使用以下行中的方法调用
<a href='message.php?msg=".$dataSet->welcomeMessage()."'>
关于php - 语法错误,PHP OOP 中出现意外的 '.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38290636/