I am having an issue calling an object's function. Can someone assist?
Error:
PHP Fatal error: Call to a member function add() on a non-object in C:\Properties.php on line 39

<?php

require_once 'HashTable.php';

/**
 * Properties
 *
 * The following libraries should be load at instantiation
 * dl("php_pdo.dll");
 * dl("php_sqlite.dll");
 */
class Properties
{
    private $hash;
    private $dbHandle;

    public function __construct($sqliteDB)
    {
        $hash= new HashTable();
        $this->dbHandle = sqlite_open($sqliteDB, 0666, $sqliteerror) or die($sqliteerror);
        $this->load();
    }
    
    function __destruct()
    {
        sqlite_close($this->dbHandle);
    }

    public function load()
    {
        if (!is_null($this->dbHandle)) {
            $result = sqlite_array_query($this->dbHandle, 'select * from prop');
            /*foreach ($result as $entry) {
                var_dump($entry);
            }*/
            foreach ($result as $entry) {
                echo "Adding...  ";
                echo $entry['key']." = ".$entry['value']."\n";
                $this->hash->add($entry['key'], $entry['value']);
            }

        } else {
            die("No active config file \n");
        }
    }
    
    private function getHash()
    {
        return $this->hash;
    }
    
    public function get($key)
    {
        return $this->$hash->get($key);
    }
}
?>
<?php
//add function from HashTable class
public function add($key= null, $value= null) {
   		if (!is_null($key) && !is_null($value)) {
	        $icomp= new IComparable($key, $value);
	        $this->addIComparable($icomp);
    	}
    }
?>

Recommended Answers

All 5 Replies

I think

foreach ($result as $entry) {

$result is null or doesn't return an array.

$this->hash->add($entry['key'], $entry['value']);

try getting rid of
this-> on line 39
so all you have is

$hash->add($entry['key'], $entry['value']);

and getting rid of $this-> on line 54

nav33n:
$result and $entry are valid. When I uncomment lines 33-35 and dump a valid array is revealed.

array(4) {
  [0]=>
  string(6) "dbname"
  ["key"]=>
  string(6) "dbname"
  [1]=>
  string(6) "config"
  ["value"]=>
  string(6) "config"
}
array(4) {
  [0]=>
  string(6) "dbname"
  ["key"]=>
  string(6) "dbname"
  [1]=>
  string(6) "config"
  ["value"]=>
  string(6) "config"
}
array(4) {
  [0]=>
  string(6) "dbserv"
  ["key"]=>
  string(6) "dbserv"
  [1]=>
  string(10) "dbserv.com"
  ["value"]=>
  string(10) "dbserv.com"
}
array(4) {
  [0]=>
  string(6) "dbport"
  ["key"]=>
  string(6) "dbport"
  [1]=>
  string(4) "4243"
  ["value"]=>
  string(4) "4243"
}

R0bb0b:
When I make your change I get the following error:
PHP Notice: Undefined variable: hash in C:\Properties.php on line 39
PHP Fatal error: Call to a member function add() on a non-object in C:\Properties.php on line 39

Hear is my tester:

<html>
<body>
<pre>
<?php
require_once 'Properties.php';

dl("php_pdo.dll");
dl("php_sqlite.dll");

$dbname = "config";
$props = new Properties("config");
?>
</pre>
<hr />
<h3>Properties</h3>
DB Server: <?php echo $props->get('dbserv')."<br>\n" ?>
DB Port: <?php echo $props->get('dbport')."<br>" ?>
</body>
</html>

Thanks for all of your help but I resolved my issue. Line 19 should be:
$this->hash= new HashTable(); and not
$hash= new HashTable();
My code now works.

Oh, OK, that makes sense cause otherwise you're stuck in that function's scope. Sweet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.