Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.