Log in

View Full Version : Fatal error: Call to a member function query() on a non-object in D:\wokspace\rts_sea



dipika
08-01-2011, 06:30 AM
<?php
class DB{

private $link;

public static function connect($host,$user,$passwd,$dbname) {
$db=new DB();
$db->link = new mysqli($host,$user,$passwd,$dbname);
return $db;
}
public function query($sql) {
return $this->link->query($sql);
}

}
?>
iam getting such error for above code

JShor
08-01-2011, 06:54 AM
You can't create an instance of the same class within itself that you are defining. In this case, you're referencing the class DB within the function connect() which is inside the DB class.

Why don't you try creating the instance after the class and defining the variables as constructors?



<?php
class DB{

private $link;

public static function connect($host,$user,$passwd,$dbname) {
$this->link = new mysqli($host,$user,$passwd,$dbname);
return $this->link;
}

public function query($sql) {
return $this->link->query($sql);
}

}

$db = new DB();
$db->connect("", "", "", "");

?>