Jump to content

Classes Help


Motig

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/183443-classes-help/
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/183443-classes-help/#findComment-968298
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.