2

Possible Duplicate:
Fatal error: Call to a member function query() on a non-object in

I have a class file, lets call it 'stuff'

Stuff.class

inside there is a class

class  Stuff {

and in there I have a public static function

   public static function morestuff() {

}

Inside I need to call another function for a query

$q="Select from contacts where id =". $this->$db->escapeVal($ID)".";

But I get errors.

$q="Select from contacts where id =". escapeVal($ID)".";

Returns

Call to undefined function escapeVal()


$q="Select from contacts where id =". $db->escapeVal($ID)".";

Returns

Call to a member function escapeVal() on a non-object


$q="Select from contacts where id =". $this->$db->escapeVal($ID)".";

Returns

Using $this when not in object context 

So what do I put?

EDIT:

A similar function in the same file has the following code

'id' = '" . $this->db->escapeVal($this->_Id) . "'

However when I try to use this code in my mysql query I get the following error

Using $this when not in object context
Community
  • 1
  • 1
Web Master
  • 4,240
  • 6
  • 20
  • 28
  • Probably unrelated to the problem, but you need `$this->db` and not `$this->$db`. – Madara's Ghost Jul 09 '12 at 19:32
  • Thanks, I just noticed that also. – Web Master Jul 09 '12 at 19:35
  • 1
    Instead of alternating between code and English, present uninterrupted, complete, concise, representative [sample code](http://sscce.org/) so that it can be given directly to PHP and run as-is. If you need to comment, use comments. – outis Jul 09 '12 at 19:45
  • possible duplicate of [Call to a member function prepare() on a non-object PHP Help](http://stackoverflow.com/q/4463441/), [Fatal error: Call to a member function query() on a non-object in](http://stackoverflow.com/q/7258875/), [Another “Yet another call to a member function function() on a non-object”](http://stackoverflow.com/q/3568785/) – outis Jul 09 '12 at 19:49

4 Answers4

0

I suspect that $db is a global variable, so try

public static function morestuff() {
      global $db;

      $q="Select from contacts where id =". $db->escapeVal($ID);

}

If the function is in the same class and is also static you will call it like

self::escapeVal($ID);
Sabeen Malik
  • 10,816
  • 4
  • 33
  • 50
  • I suggest you inject it as an argument, and not use global! – Madara's Ghost Jul 09 '12 at 19:30
  • @Truth you should suggest that to the poster and not penalize me for that! – Sabeen Malik Jul 09 '12 at 19:33
  • I updated the question with info you might find useful @SabeenMalik I just upvoted you, don't worry – Web Master Jul 09 '12 at 19:34
  • @SabeenMalik: No, **You** are the one providing the solution, so **You** should be pointing what seems the correct solution in your eyes. If using `global` seems correct to you, then you definitely deserve the downvote. If not, correct your post and I will remove my vote. That simple :) – Madara's Ghost Jul 09 '12 at 19:35
  • @Truth you 'suggested' not using global but from the looks of it you are are forcing it not just suggesting it. – Sabeen Malik Jul 09 '12 at 19:39
0

You are not understanding the concept of OOP. $this is a variable which refers to the current object (or this object). Meaning:

class Test {
    public function __construct() { //Called when object is instantiated
        var_dump($this);
    }
}
$test = new Test();

You'll get something similar to

object(Test)#1 (0) { }

You cannot use $this outside of a class method. It just doesn't work that way.

About the error you're having, try to figure out where is the database connection stored. That connection should either be passed to the object for storing as a field, or directly to the method to work with it internally.

Programming isn't about copy/pasting code that works.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

Because your function is static, the variable $this won't exist inside it.

To solve your problem, there are 2 solutions:

  1. Make $db a static variable, meaning it will be the same in every instance.
  2. Remove the static keyword from the function morestuff(). In this case you will need to make an instance of the class in order to make a call to morestuff().
Mike.
  • 83
  • 5
0
$Stuff->db->escapeVal($id)

started to work. So happy i got it to work, Thank you very one.

Web Master
  • 4,240
  • 6
  • 20
  • 28