Skip to content

Instantly share code, notes, and snippets.

@nielsmh
Created April 23, 2013 16:08
Show Gist options
  • Save nielsmh/5444959 to your computer and use it in GitHub Desktop.
Save nielsmh/5444959 to your computer and use it in GitHub Desktop.
Crazy PHP. Consistency, don't need that. You can apply the "new" operator to a variable containing a string, but you can't apply it to a string literal. Why are those two different at all?
$ php -a
Interactive shell
php > class Lol { }
php > $a = new Lol;
php > var_dump($a);
object(Lol)#1 (0) {
}
php > $b = new 'Lol';
PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in php shell code on line 1
php > $b = 'Lol';
php > $bb = new $b;
php > var_dump($bb);
object(Lol)#2 (0) {
}
php > $c = 1;
php > $cc = new $c;
PHP Fatal error: Class name must be a valid object or a string in php shell code on line 1
$ _
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment