quickstopman Posted December 10, 2007 Share Posted December 10, 2007 hi guys im new too using classes and objects to program stuff i made a little bit of code but i keep getting weird errors what exactly am i doing wrong <? class user_traits { private function Connect($id) { $id; $query = mysql_query("SELECT * FROM users WHERE id = '{$id}'")or die(mysql_error()); $row = mysql_fetch_array($query); } public function GrabUserInfo() { $this->Connect($id); $this->id = $id; $this->username = $row['username']; $this->password = $row['password']; $this->aboutme = $row['aboutme']; $this->age = $row['age']; $this->email = $row['email']; $this->ms_email = $row['ms_email']; $this->ms_passsword = $row['ms_password']; $this->yt_username = $row['yt_username']; $this->yt_password = $row['yt_password']; $this->del_username = $row['del_username']; $this->del_password = $row['del_password']; } public function PostComment() { /* Yet to be Coded */ } } $user = new user_traits; $user->GrabUserInfo(12); echo $user->id; ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 What are the errors? Also, you aren't defining your variables <?php class user_traits { var $id; var $password; //...etc Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 10, 2007 Share Posted December 10, 2007 Your error is due to variable scope. A variable declared in one method is not visible in a different method anymore than the same variable declared in one function is visible in another function. Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 10, 2007 Share Posted December 10, 2007 Also... Classes are usually defined with CapsCase, whilst functions (methods) are camelCase. Plus, you're mixing your underscore_case with camelCase. To the class now: Connect doesn't return a value, or set an instance variable: $row = mysql_fetch_array($query); // grab the data $this->user = $row; // set an instance variable // or, you could have done this: return $row // return the row to the GrabUserInfo() function so how is $row defined in GrabUserInfo() ? Is Connect() the most appropriate method name? It's not actually connecting, is it? Next, is there any point in replicating $this->property = $row['property'] for each and every column? You could just store the mysql result as a variable: $user->props->['property'] or iterate with a for loop to set instance variables: foreach($row as $key => $value){ $this->$$key = $value; } Also, it's good practice (standard) to specify your class variables in the head of your class rather than set them in a class method. In many languages this would throw an error! The method PostComment() is not really a property of user_traits is it? You would be best off having this as a method of another class, perhaps a User class. I know that seems pedantic but that's teh thing with OOP - unless you're clear in your thinking, you may as well go back to procedural / spaghetti code! I hope you don't take this as a barrage of criticism... you can pick out the technical from the practical and go from there. Cheers, Dave Quote Link to comment Share on other sites More sharing options...
emehrkay Posted December 11, 2007 Share Posted December 11, 2007 Your error is due to variable scope. A variable declared in one method is not visible in a different method anymore than the same variable declared in one function is visible in another function. ... $row as defined in Connect() is not accessible in GrabUserInfo(). you can fix it a few ways. in GrabUserInfo() do $row = $this->Connect(); //in your connect method you have to return $row or in Connect() set $row as a property of the class $this->row = mysql_fetch_array($query); and in GrabUserInfo() use it as $this->row 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.