0

$db=new DataBase();

It is a object I created globaly. For accessing this in a function , I used global keyword. Here is the code:

include "Database.php";
$db=new DataBase();

function getUser()
{
    $uname=$_SESSION['UNAME'];
    global $db,$uid;
    $result2=$db->selectUserDetails();
    $result3=$db->selectUserPermission($uid);
    $table =   constructTable($result2, $result3);
    echo $table;
}

and when i am using that, it is showing an error

"Fatal error:  Call to a member function selectUserDetails() on a non-object 
in C:\\wamp\\www\\listdetails.php on line 27" 

Anyone can tell me a solution.

vusan
  • 5,221
  • 4
  • 46
  • 81
aji136252
  • 13
  • 1
  • 7
  • write `global $db,$uid;` on very first line of your function `getUser()` – GBD Dec 11 '12 at 08:59
  • You are showing us a function, but aren't showing us how the function is called. It sounds like `$db` doesn't exist at the time that the function is called... – Charles Dec 11 '12 at 09:00
  • no ,it is showing the same error – aji136252 Dec 11 '12 at 09:02
  • 2
    As an aside, I will just point out that using globals is lazy and considered bad form. It forces anyone working with your code to have to work that much harder. Consider using proper OOP principles in the use of objects. The problem here is that you are mixing procedural code with object-oriented code. `getUser()` should be a method in a class. Beyond that minor criticism, why not just pass `$db` into the function? – Ian Atkin Dec 11 '12 at 09:04
  • thanks charles ..U said it... – aji136252 Dec 11 '12 at 09:05
  • Can you please show us what's in the file `C:\wamp\www\listdetails.php` (because the error occurs on `line 27` of that file). We need to see where you call the function and if it happens before you instantiate $db. – Sherif Dec 11 '12 at 09:10
  • 1
    [Please don't use `global`s in your code for the better of humanity](http://stackoverflow.com/questions/11923272/use-global-variables-in-a-class/11923384#11923384). – PeeHaa Dec 11 '12 at 09:26
  • @aji136252 please refer to my answer. – Kelly Copley Dec 11 '12 at 09:26
  • Well since you have to start somewhere.. maybe a $db instanceof DataBase check before your function, and after the global $db declaration.. At least then you know if you have a properly instantiated DataBase Object to begin with. – Kelly Copley Dec 11 '12 at 09:53

1 Answers1

-1

This sounds to me like you are calling a static function in a non static context..

I think you need to nix the new DataBase() and just call the function statically;

$result2=$db::selectUserDetails();
Kelly Copley
  • 2,990
  • 19
  • 25
  • You get different messages than "on non-object" when accessing a) a private method or b) a static method like $obj->staticMethod, a) results in "Fatal error: Call to private method ..." and b) in no message at all (not even with error_reporting(2147483647) – VolkerK Dec 11 '12 at 09:39
  • The OP's error message is clear. It is the result of calling a method on a non-object. Calling a static method from object context is actually allowed as long as the method does not reference $this. [See here](http://ideone.com/S9dQmI). Sorry, but your answer does not address the OP's question. – Sherif Dec 11 '12 at 09:41
  • go home kelly, you are drunk – codefreak Dec 11 '12 at 09:48
  • drunk? nope.. Sleep deprived that's a huge possibility. – Kelly Copley Dec 11 '12 at 09:50