Im rewriting application from .NET to PHP. I need to create class like this:
class myClass
{
public ${\'property-name-with-minus-signs\'} = 5;
public
You cannot use hyphens (dashes) in PHP class properties.
PHP variable names, class properties, function names and method names must begin with a letter or underscore ([A-Za-z_])
and may be followed by any number of digits ([0-9])
.
You can get around this limitation by using member overloading:
class foo
{
private $_data = array(
'some-foo' => 4,
);
public function __get($name) {
if (isset($this->_data[$name])) {
return $this->_data[$name];
}
return NULL;
}
public function __set($name, $value) {
$this->_data[$name] = $value;
}
}
$foo = new foo();
var_dump($foo->{'some-foo'});
$foo->{'another-var'} = 10;
var_dump($foo->{'another-var'});
However, I would heavily discourage this method as it is very intensive and just generally a bad way to program. Variables and members with dashes are not common in either PHP or .NET as has been pointed out.