-1

I'm working with a few different classes across different php files. For some reason, the first time I call my function, it works correctly, but the second call results in an error.

This is my "something" class (edited for public view)

class something{
    private $db;
    function myfunction($sql){
        $db = new db();
        $results = $db->select("sql query using $sql");
        return(empty($results[0]['name'])?0:1);
     }
}

"db" is another class I'm using to handle the database queries etc. I don't believe the issue lies in this file since the first call works correctly.

Here's the code I'm using to test it. This is in a different php file from the "something" class.

$something = new something();
echo($something->myfunction('string'));
echo($something->myfunction('string2'));

As I mentioned, the first call ("string") works correctly, but the second one ("string2") fails. This is the error I get.

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

I don't believe it's an issue with the select function (or the db class) since the first call works correctly. I think the issue lies with me declaring $db in "myfunction", but I'm not sure why.

Justin
  • 93
  • 1
  • 11
  • this is bizzar, at-least for me. Have you tried working with the second output only, alone by it-self? – samayo May 17 '13 at 18:47
  • Does the error occur with `$something->myfunction` or `$db->select`? – silkfire May 17 '13 at 18:48
  • Yes. The second call works correctly if the first one is commented out. In other words, there's only an error when I call the function more than one time. And the error is within "myfunction", I think it's because of the way I'm declaring "db", but I'm not sure how to fix it. – Justin May 17 '13 at 18:53

3 Answers3

0

What kind of DB wrapper are you using? Is database persistence turned on? It migth be disconnecting and not reconnecting the second time round, thus returning an empty DB object.

Maxim Kumpan
  • 2,545
  • 2
  • 19
  • 23
0

Can I assume that this is a short version of your code, and you have some logic to instantiate $db only once? In that case you must change that line from

$db = new db();

to

$this->db = new db();

Otherwise db object will be gone at the end of the call.

George Cummins
  • 28,485
  • 8
  • 71
  • 90
Zsolt Szilagyi
  • 4,741
  • 4
  • 28
  • 44
0

First off, I would shy away from creating a new $db every time you call that function unless you're going to explicity destroy it at the end of the function call. Secondly, you are creating the class variable private $db but you aren't assigning the db object to it. You should try the following:

class something{
    private $db;
    function myfunction($sql){
        if(!isset($this->db)){
            $this->db = new db();
        }
        $results = $db->select("sql query using $sql");
        return(empty($results[0]['name'])?0:1);
     }
}

I feel like that should work better. Let me know!


Edit:

Right- so either do it as my code above shows, or use the class constructor to do it. In PHP the constructor object isn't the class name, it is defined as __construct() My approach would be either to extend the DB class as I mentioned in my comment above, or to do it in the constructor. My reasoning behind that is because I just don't like creating class-instances inside of methods if I can avoid it, I'd rather make it once when the object is created. SOoooooOOOoOOoOOo:

class something{
    private $db;
    public function __construct(){
        $this->db = new db();
    }

    function myfunction($sql){
        $results = $this->db->select("sql query using $sql");
        return(empty($results[0]['name'])?0:1);
     }
}

Notice the usage of $this-> That's a PHP thing. Get familiar with it if you aren't- you'll be using it a BUNCH. :) Good luck!

dudewad
  • 13,215
  • 6
  • 37
  • 46
  • Also maybe you could consider extending the DB object with the something() class if the DB functionality is a critical part of something()'s functionality? – dudewad May 17 '13 at 18:55
  • How else could I declare it? In Java I would change `private $db;` to `private $db = new db();` But that doesn't seem to work in PHP. Is there any ways to declare it outside of the function? – Justin May 17 '13 at 18:58
  • Editing my response so I can add code... – dudewad May 17 '13 at 19:07
  • Fixed a couple other issues and added this->. It's working now. Thanks! Still getting used to OOP in PHP (coming from Java), and it's a bit of a challenge, so thanks for the help :) – Justin May 17 '13 at 19:19