5

Im rewriting application from .NET to PHP. I need to create class like this:

class myClass
{
    public ${'property-name-with-minus-signs'} = 5;
    public {'i-have-a-lot-of-this'} = 5; //tried with "$" and without
}

But it doesnt work. I dont want to use something like this:

$myClass = new stdClass();
$myClass->{'blah-blah'};

Because i have a lot of this in code.

Edit few days later: i was writing application that uses SOAP. These fancy names are used in API which i had to communicate with.

Kamil
  • 13,363
  • 24
  • 88
  • 183
  • 2
    Why do you need the curly braces? What's wrong with just doing `public $property-name-with-minus-signs = 5`? – Bojangles Feb 17 '12 at 03:05
  • 5
    @Jam Uhm... it doesn't work? :) – deceze Feb 17 '12 at 03:09
  • 2
    @JamWaffles: Maybe... the syntax error? :) – Ry- Feb 17 '12 at 03:09
  • 1
    @deceze I will leave my comment up as a self-shaming. I know you can use underscores but it never occurred to me that a dash might be a minus sign! It's 3:10am. I should go to bed. – Bojangles Feb 17 '12 at 03:10
  • 5
    @Kamil Why do you *need* properties with dashes in the first place? PHP isn't .NET, and dashes in variable or property names are uncommon in the PHP world (guess why; because they don't work). CamelCasing is the usual way to write those. – deceze Feb 17 '12 at 03:12
  • 3
    @deceze: I don't think you can use dashes in identifiers in *any* .NET language, either. – Ry- Feb 17 '12 at 03:16
  • @Kamil be sure to select the answer that solved your question. – Highway of Life Feb 18 '12 at 09:05
  • @JamWaffles minus sign is interpreted as substract operation. – Kamil Feb 21 '12 at 16:37

3 Answers3

11

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.

Highway of Life
  • 22,803
  • 16
  • 52
  • 80
  • 1
    As i wrote - they works (as dynamic properties, but i need to clone that class and i need default value in cloned class. – Kamil Feb 17 '12 at 03:25
  • 1
    @Kamil Set default in the constructor. Or I have a good idea! Don't use identifiers that contain a hyphen! :) – kapa Feb 17 '12 at 10:01
7

I used code like this:

class myClass
{

    function __construct() {

        // i had to initialize class with some default values
        $this->{'fvalue-string'} = '';
        $this->{'fvalue-int'} = 0;
        $this->{'fvalue-float'} = 0;
        $this->{'fvalue-image'} = 0;
        $this->{'fvalue-datetime'} = 0;   
    }
}
Kamil
  • 13,363
  • 24
  • 88
  • 183
1

You can use the __get magic method to achieve this, although it may become inconvenient, depending on the purpose:

class MyClass {
    private $properties = array(
        'property-name-with-minus-signs' => 5
    );

    public function __get($prop) {
        if(isset($this->properties[$prop])) {
            return $this->properties[$prop];
        }

        throw new Exception("Property $prop does not exist.");
    }
}

It should work well for your purposes, however, considering that -s aren't allowed in identifiers in most .NET languages anyway and you're probably using an indexer, which is analogous to __get.

Ry-
  • 218,210
  • 55
  • 464
  • 476