Motig Posted November 30, 2009 Share Posted November 30, 2009 Hi all, I'm new to classes in php, though I'm used to them in other languages. Not seeing what I'm doing wrong here. I get the error "Cannot access empty property". My code: $user = new Player(0); echo $user->$name; And the class itself: class Player { public $id = 0; public $name = "???"; public $pass = ""; public function __construct($f_id) { if ($f_id == 0) return; //there is other stuff under this but is irrelevant } } Surely $user->$name should be "???"? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/183443-classes-help/ Share on other sites More sharing options...
mikesta707 Posted November 30, 2009 Share Posted November 30, 2009 it should be $user->name; using what you had makes PHP think you are using a variable variables (instead of accessing the name attribute, you are trying to access the attribute whose name is the value of $name. For example if $name is equal to "pass" then saying $user->$name is the same as saying $user->pass) Quote Link to comment https://forums.phpfreaks.com/topic/183443-classes-help/#findComment-968298 Share on other sites More sharing options...
Motig Posted November 30, 2009 Author Share Posted November 30, 2009 Ah, right, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/183443-classes-help/#findComment-968314 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.