Alex Rivera | Logout

PHP variables in classes

Asked 2009-08-18T09:58:58.163
9

I have the following code (I'm a Dot Net developers and I thought if I can bring my OOP knowledge to PHP)

class user {
    var $_un;
    function user($un) {
     $_un = $un;
    }
    function adduser() {
    }
    function checkuser() {
    }
    function printuser () {
        echo $_un;
    }
}

$newuser = new user('Omar Abid');
$newuser->printuser();

So the problem is simple "$_un" is empty!! I want it to be filled, when the class is created using the constructor and then saved.

I used to do that in C# .net, but here it doesn't work for some reasons. Thanks!

Edit
Report

1 Answer

3

in php you have to use $this to access variables (or methods) of the current class, this is necessary to distinguish class members from local variables

answered 2009-08-18T10:07:08.803

Your Answer