Kev0121 Posted April 17, 2009 Share Posted April 17, 2009 Hello, well ive created a database class which has simple mysql functions like, query, numrows, fetch_array and ive created a new class in a seperate file called users and i created a new instance of the database class $db = new Database(); but when i use it in my user class it doesnt work, for example require_once 'database.php'; class User { var $user; var $pass; function select_a() { $sql = "SELECT * FROM users"; $res = $db->query($sql); } } Helllp please =D /Kev Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted April 17, 2009 Share Posted April 17, 2009 Google "function scope", it should tell you a bit of information to help out. Quote Link to comment Share on other sites More sharing options...
Kev0121 Posted April 17, 2009 Author Share Posted April 17, 2009 What am i looking for? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 17, 2009 Share Posted April 17, 2009 encapsulation... methods and properties of your class belong to that class - anything declared outside must be passed in... <?php require_once 'database.php'; class User { var $user; var $pass; private function User($db) { $this->db = $db; } function select_a() { $sql = "SELECT * FROM users"; $res = $this->db->query($sql); } } $user = new User($db); ?> if you have php 5 available then you could use __construct() - it wold also be beneficial to make the database object a singleton class that way you won't have lots of instances of he 'same thing'... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.