0

I'm working on a simple project and I'm using PHP Object Oriented. Basically I have a class that contains information about how to get data from MySQL database and show them on a page. This class is called admin.php and goes like this:

<?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;
    }
}
?>

Then on another page which is called dashboard.php ,I have included this class file and coded this:

<?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>"; 
    }
?>

But whenever I run it ,I get this error message:

Parse error: syntax error, unexpected '.' on line 15 in dasboard.php

Here's the line 15 of the dasboard file:

<a href='message.php?msg=".$dataSet->msg();."'>

Pretty sure that this must be related to the wrong concatenating and combining a class method within a html tag while I'm echoing out a statement.

So what's the correct way to do this ?

  • Then it would return a Fatal error: Call to undefined method Admin::msg() ! However it is defined correctly as you can see in the code –  Jul 10 '16 at 09:57
  • We can see it is not defined, you might be wanting to call the method named `welcomeMessage()`. Also why echo the HTML within PHP quotes, just output plain HTML. One more thing: this class contains code for different responsibilities. You should seperate these concerns. – JasonK Jul 10 '16 at 10:03
  • The error is on `;.`. Also, `msg` is a private property, not a method. – Ismael Miguel Jul 10 '16 at 10:12

2 Answers2

0

"$dataSet->msg" is not a function but a property, and you need to call it like this.

<a href='message.php?msg=".$dataSet->msg."'> // but wont work as it is private

or use the method call as in the below line

<a href='message.php?msg=".$dataSet->welcomeMessage()."'>
kuma DK
  • 1,812
  • 1
  • 18
  • 36
  • And also there wont be any use of these calls sins you are getting into the if block only when the "welcomeMessage()== 0" and even in the echo block its going to remain "0" – kuma DK Jul 10 '16 at 10:10
0

Ok, let supoose that $dataSet is defined, and it has all required methods. Then you just need to omit the ;:

echo "some text " . $dataSet->msg() . " more text";
michaJlS
  • 2,465
  • 1
  • 16
  • 22