2

I want to create a new object using this

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

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

5
  • 1
    I thin it is because of extra $ before {. Commented Mar 25, 2012 at 20:33
  • @tereško That's a variable variable. Commented Mar 25, 2012 at 20:34
  • @Michael No, '\' is invalid syntax, as for "\\" it doesn't make a difference. Commented Mar 25, 2012 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. Commented Mar 25, 2012 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 Commented Mar 25, 2012 at 20:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I just want to understand. Besides, the first one is more compact.
@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.
Yeah. however This is still is self-modifing code which is an awful idea to begin with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.