Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Class help

  1. #1
    New Coder
    Join Date
    Nov 2012
    Location
    United Kingdom
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #3
    New Coder
    Join Date
    Nov 2012
    Location
    United Kingdom
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •