I'm trying to make a class which has object names with a space in between. Just so I can access them like this:
$object->{'Var name with spaces'};
I've read this topic over here and now know how to access those variables, but I'm not sure how to create a class with these variables. I've tried something like this, but I can't get it to work:
class Hotel
{
public ${'Var name with spaces'} = 'Some value'; // Fails
}
How would I go and create variables in a class definition containing spaces?
I'm pretty sure you'd have to do this in your constructor, because class properties cannot require runtime evaluation.
class Hotel {
public function __construct() {
$this->{'Var name with spaces'} = 'Some value';
}
}
You can see it working in this demo, but I think the comments have shed enough light on why you shouldn't do this. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With