Jump to content

[SOLVED] New to OOP


quickstopman

Recommended Posts

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;
?>

Link to comment
https://forums.phpfreaks.com/topic/81072-solved-new-to-oop/
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/81072-solved-new-to-oop/#findComment-411453
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/81072-solved-new-to-oop/#findComment-411919
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.