1

I need to create a property called "aip-aup" in a class but I can't get it to work.

First tried to put it up with the property definitions. Unfortunately bracketed property-definitions are not allowed.

This fails (in the __construct()):

$this->${'aip-aup'} = array();

Gives error "Undefined variable 'aip-aup'".

This as well (in the __set method):

$this->${'aip-aup'}[$property] = $value;

Also tried creating a custom helper method, but does absolutely nothing:

$this->createProperty('aip-aup', array());

Any help here?

The property has to be public so should be doable?

silkfire
  • 24,585
  • 15
  • 82
  • 105
  • This goes against OOP practices. "aip-aup" is also a terrible name for a property because of the dash. – Halcyon Aug 06 '13 at 08:45
  • I want my hyphen there =/ Mostly also exploring the limits and possibilities of PHP. – silkfire Aug 06 '13 at 08:45
  • `$this->{'aip-aup'} = array();` – Mark Baker Aug 06 '13 at 08:45
  • 1
    These type of dynamics usually implement magic getters and setters, any reason you can't do the same here? – Flosculus Aug 06 '13 at 08:53
  • @MarkBaker Lol, I had a dollar sign in front of the bracket. At first I thought your reply was redundant, but after closer inspection I realized how simple and brilliant it was. Case closed. Thanks. – silkfire Aug 06 '13 at 08:54

1 Answers1

1

If you need doing something like this, then you are doing something wrong, and it would be wise to change your idea, than trying to hack PHP.

But if you have to, you can try this:

class Test {
    public $variable = array('foo'=>'bar');

public function __get($name){
    if ($name == 'aip-aup'){
        return $this->variable;
        }
    }
}   

$test = new Test();
$func = 'aip-aup';
$yourArray = $test->$func;
echo $yourArray['foo'];
Volvox
  • 1,639
  • 1
  • 12
  • 10