2

I want to create a new object using this

$procedure = new ${$s.'\\'.$p};

It doesn't work. Why isn't this possible?

hakre
  • 193,403
  • 52
  • 435
  • 836
x74x61
  • 423
  • 6
  • 18
  • 1
    I thin it is because of extra `$` before `{`. – tereško Mar 25 '12 at 20:33
  • @tereško That's a [variable variable](http://us3.php.net/manual/en/language.variables.variable.php). – Michael Berkowski Mar 25 '12 at 20:34
  • @Michael No, '\' is invalid syntax, as for "\\" it doesn't make a difference. – x74x61 Mar 25 '12 at 20:37
  • @Michael, `"\"` is also the escape character. If you do single slash it will escape `'` or `"` of the string and will cause a parse error. – Shoe Mar 25 '12 at 20:38
  • @Michael , in that case it would be an extremely bad practice ... i try to assume unintentional stupid mistake , before intentional harmful code convention – tereško Mar 25 '12 at 20:43

1 Answers1

2

Why don't you

$name = "$s\\$p";
$procedure = new $name;

?

Also ${$s.'\\'.$p} means a variable, with a variable name that is clearly not good. If you are, and I think you are, trying to get something like an instance of Namespace\Class you should try with the code below.

I think that the {} shortcut only works with this syntax ${} which is clearly referring to a variable. So you cannot use it for instantiating new objects.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • I just want to understand. Besides, the first one is more compact. – x74x61 Mar 25 '12 at 20:35
  • @x74x61, It have a strange behavior probably because of some problem in the namespace. I tried also with `new {$s.'\\'.$p}` but didn't work. If you have to use this thing so many times that you need a shortcut, maybe there's something wrong in your application. – Shoe Mar 25 '12 at 20:46
  • 2
    Yeah. however This is still is self-modifing code which is an awful idea to begin with. – Cristian Rodriguez Mar 25 '12 at 20:46