Author Topic: [SOLVED] New to OOP  (Read 595 times)

0 Members and 1 Guest are viewing this topic.

Offline quickstopmanTopic starter

  • Enthusiast
  • Posts: 199
  • Gender: Male
  • I am, who i am...
    • View Profile
    • Untitled6
[SOLVED] New to OOP
« on: December 10, 2007, 04:03:47 PM »
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
Code: [Select]
<?
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;
?>
14 years old // 5 Sites // 3 Complete

Offline pocobueno1388

  • Fanatic
  • Posts: 3,368
  • Gender: Male
    • View Profile
    • PHPify
Re: New to OOP
« Reply #1 on: December 10, 2007, 05:45:02 PM »
What are the errors?

Also, you aren't defining your variables

Code: [Select]
<?php

class user_traits {
   var 
$id;
   var 
$password;
   
//...etc


Offline roopurt18

  • Guru
  • Fanatic
  • *
  • Posts: 3,658
  • Gender: Male
  • le sigh
    • View Profile
    • rbredlau
Re: New to OOP
« Reply #2 on: December 10, 2007, 06:01:18 PM »
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.

Offline davestewart

  • Enthusiast
  • Posts: 68
    • View Profile
Re: New to OOP
« Reply #3 on: December 10, 2007, 06:15:44 PM »
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:

Code: [Select]
$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:

Code: [Select]
$user->props->['property']
or iterate with a for loop to set instance variables:

Code: [Select]
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

Offline emehrkay

  • Guru
  • Addict
  • *
  • Posts: 1,853
    • View Profile
Re: New to OOP
« Reply #4 on: December 11, 2007, 09:37:10 AM »
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