3

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.

2
  • Side note: You will save yourself a few lines if you do a __call($name) method and remove all those methods that call variables. Inside the magic method, you can do $split = preg_split('/(?=[A-Z])/', $name); $var = strtolower($split[1].'_'.$split[2]); return $this->{$var}; so that when you do $obj->getSiteName(); it will dynamically return $this->site_name. Saves you from writing a whole bunch of methods that mechanically do the exact same thing. Something to think about. Commented Aug 4, 2016 at 19:45
  • I think it's awesome that you are jumping right in with OOP. Go for it! Commented Aug 27, 2016 at 12:50

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.