Undefined property

Hey,

I can’t seem to work out what’s wrong with this code. I am trying to populate a <select>.

This is the code:


class Category {
    public $ID, $name;

    public function __construct($dbrow) {
        $this->ID = $dbrow['ID'];
        $this->name = $dbrow['name'];
    }

    public function getcatID(){
        return $this->ID;
    }

    public function getName(){
        return $this->name;
    }
}


<?php
require_once('Classes.php');

class Items {

    public function __construct($host, $username, $password, $dbname) {
        $this->mysqli = new mysqli($host, $username, $password, $dbname);
        if ($this->mysqli->errno){
            echo 'Unable to connect'.$this->mysqli->error;
            exit();
        }
    }

    public function selectCategoryTest() {
        $sql = 'SELECT * FROM hussaini_categories
            WHERE deleted = 0';
        $results = $this->mysqli->query($sql) or die(mysqli_error());
    }
 
    public function __destruct() {
        $this->mysqli->close();
    }
}

In the controller i have:


$item_cat = $item->selectCategoryTest();
require_once('Views/add-new-item.phtml');

In my view, i have the following <select>…


                <select name="CID" id="CID">
                    <?php foreach($view->catList as $category) : 
                    if($category->getcatID() == 4){
                    echo "<option value='$category->getcatID()' selected=\\'selected\\'>$category->getName()</option>";
                    }else{
                    echo "<option value='$category->getcatID()'>$category->getName()</option>";
                    }
                    endforeach;
                    ?>
                </select>

But i get an error saying:

<b>Notice</b>: Undefined property: Category::$getcatID in <b>G:\xampp\htdocs\Manstore\Views\add-new-item.phtml</b> on line <b>19</b>

So it doesn’t understand $category->getName() OR $category->gecatID()

I am just testing with an ID of 4 for the time being…

Any ideas why it doesn’t work??

Lot’s of info here
http://www.php.net/manual/en/language.types.string.php

The summary is you need to use the complex syntax with curly braces if you want to embed method calls into strings. Like


echo "         {$obj->foo()}     ";

Otherwise, php interprets it as $obj->foo

Personally, I like to put variables into strings, but I tend to stay away from method calls. I usually either use something from the printf() family, or just store the result of the method call into a variable, and put the regular plain variable into the string. You can also just break out of the string and concatenate.