houdriwiokug houdriwiokug - 4 months ago 7
MySQL Question

Undefined property error message in PHP OOP

I'm working on a custom CMS using PHP OOP and this is actually my first project ever which is made with object oriented programming so I don't have that much of experience with it. Basically I have a class called

Site.class.php
which retrieves data from one of the tables in MySQL database and goes like this:

<?php
class Site
{
public $id,$site_name,$site_title,$site_url,$site_tags,$site_desc;
public function __construct()
{
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function getSite($name)
{
if(!empty($name))
{
$site = $this->db->prepare("select * from admins where site_name = ?");
$site->bindParam(1,$name);
$site->execute();
while($row = $site->fetch())
{
$this->id = $row['id'];
$this->site_name = $row['site_name'];
$this->site_title = $row['site_title'];
$this->site_url = $row['site_url'];
$this->site_tags = $row['site_tags'];
$this->site_desc = $row['site_desc'];
}
}
else
{
header("Location: maint/php/includes/errors/005.php");
exit();
}
}
public function getID()
{
return $this->id;
}
public function getSiteName()
{
return $this->site_name;
}
public function getSiteTitle()
{
return $this->site_title;
}
public function getSiteUrl()
{
return $this->site_url;
}
public function getSiteTags()
{
return $this->site_tags;
}
public function getSiteDesc()
{
return $this->site_desc;
}
}
?>


I have included this file at another file which is called
settings.php
and called it in this way:

$siteSet = new Site();
$siteSet->getSite("Daygostar");


Then I tried echoing out the variables like this:

<div class="box-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="usr">Site Name:</label>
<input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteName; ?>">
</div>
<div class="form-group">
<label for="usr">User URL:</label>
<input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteUrl; ?>">
</div>
</div>
</div>
</div>


But the problem is that whenever I call this file ,I receive this error message:


Undefined property: Site::$getSiteName

Undefined property: Site::$getSiteUrl


I don't know what's really going wrong because I have coded everything correctly! So if you know how to solve this question please let me know, I really appreciate that.. Thanks in advance.

Answer

Those are both methods. You need to add the () to the end of them to invoke the method.

<div class="box-body">
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="usr">Site Name:</label>
                            <input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteName(); ?>">
                        </div>
                        <div class="form-group">
                            <label for="usr">User URL:</label>
                            <input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteUrl(); ?>">
                        </div>
                    </div>
                </div>
            </div>
Comments