Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-03-2012, 10:48 PM   PM User | #1
Clawed
New Coder

 
Join Date: Nov 2012
Location: United Kingdom
Posts: 29
Thanks: 3
Thanked 0 Times in 0 Posts
Clawed is an unknown quantity at this point
Class help

PHP Code:
class User {

public 
$logged false;

public function 
__construct() {

global 
$db;

if( isset( 
$_SESSION['BL']['USER']['USERNAME'] ) and isset( $_SESSION['BL']['USER']['PASSWORD'] ) ) {

$user $_SESSION['BL']['USER']['USERNAME'];
$pass $_SESSION['BL']['USER']['PASSWORD'];

if( 
$this->validate$user$pass ) ) {

$this->logged true;
$query $db->query"SELECT * FROM users WHERE username = '" $user "' AND password = '" $pass "'" );
$array $db->fetch_assoc$query );
foreach( 
$array as $info => $value ) {
$this->block->$info $value;
$_SESSION['BL']['USER'][strtoupper$info )] = $value;
}

echo 
"hi";

}else{

$this->logged false;
session_destroy();

}

}

}

public function 
validate$user$pass ) {

global 
$db;

$query $db->query"SELECT * FROM users WHERE username = '" $user "' AND password = '" $pass "'" );

return 
$db->num_rows$query ) > true false;

}

public function 
hasRankFuse$fuse ) {

global 
$db;

$query $db->query"SELECT * FROM rank_permissions WHERE id = '" $this->block->rank "'" );
$array $db->fetch_assoc$query );

return 
$array[$fuse] == true false;

}

}

$user = new User
Anyone know what's wrong with this?
I keep getting this line when trying to use $user->logged

Notice: Trying to get property of non-object in
Clawed is offline   Reply With Quote
Old 12-03-2012, 11:16 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,639
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You don't show where you are using $user->logged, so we can't really help with that. The error is clear though, $user isn't instantiated as an instance of User where you are trying to use it.

Don't do this: global $db; . Globalization is an absolute debugging nightmare and it compounds with the use of objects. Pass in the object (typehinted as well) to the constructor and assign it during instantiation to a member property. Unless it can be changed during a run in which case it should be passed to any method relying on it.

You shouldn't let PHP construct object properties either, otherwise you loose control of them. Same goes with why your properties should never be scoped public, especially in a datatype weak language like PHP. So where you have assignments like $this->block->$info = $value;, these should be manually assigned. Where this is used should trigger an error as block hasn't been assigned an object type (so it will trigger E_STRICT). If $info can never be pre-determined, use an array instead.
Fou-Lu is offline   Reply With Quote
Old 12-03-2012, 11:25 PM   PM User | #3
Clawed
New Coder

 
Join Date: Nov 2012
Location: United Kingdom
Posts: 29
Thanks: 3
Thanked 0 Times in 0 Posts
Clawed is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
You don't show where you are using $user->logged, so we can't really help with that. The error is clear though, $user isn't instantiated as an instance of User where you are trying to use it.

Don't do this: global $db; . Globalization is an absolute debugging nightmare and it compounds with the use of objects. Pass in the object (typehinted as well) to the constructor and assign it during instantiation to a member property. Unless it can be changed during a run in which case it should be passed to any method relying on it.

You shouldn't let PHP construct object properties either, otherwise you loose control of them. Same goes with why your properties should never be scoped public, especially in a datatype weak language like PHP. So where you have assignments like $this->block->$info = $value;, these should be manually assigned. Where this is used should trigger an error as block hasn't been assigned an object type (so it will trigger E_STRICT). If $info can never be pre-determined, use an array instead.
I was using it in another class, but i put classes in wrong order.

Problem fixed now.
Clawed is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:02 PM.


Advertisement
Log in to turn off these ads.