facebook / hhvm

A virtual machine for executing programs written in Hack.
https://hhvm.com
Other
18.19k stars 2.99k forks source link

Can nest namespaces, behavior is kinda crazy #2260

Closed fredemmott closed 4 years ago

fredemmott commented 10 years ago

This works:

<?php
namespace Foo {

  namespace Bar {
    class Baz { }
  }
}

namespace {
  new Bar\Baz();
}

it probably shouldn't - nesting namespaces is entirely forbidden in PHP5. If they're supposed to be nestable, that should probably be Foo\Bar\Baz :)

Aatch commented 10 years ago

I vote to keep the nesting behaviour, but fix it so it's sane.

paulbiss commented 10 years ago

Fix is up internally D1669100

SiebelsTim commented 9 years ago

This is inconsistent with the typechecker.

namespace Foo {
  namespace Bar {
    class Baz { }
  }
}

HHVM thinks it's \Foo\Bar\Baz, hh_client thinks it's \Bar\Baz.

lexidor commented 4 years ago

I am going over old issues on this repository, to see which ones apply to the current versions of hhvm.

Hhvm and the typechecker now correctly identify the class as \Foo\Bar\Baz.

<?hh

namespace {
    namespace Foo {
        namespace Bar {
            final class Baz {
                public function __construct() {
                    echo static::class.\PHP_EOL;
                }
            }
            function inner(): void {
                new Baz();
            }
        }
        function one_level_out(): void {
            new Bar\Baz();
        }
    }
    function top_level(): void {
        new \Foo\Bar\Baz();
    }
    <<__EntryPoint>>
    function entrypoint(): void {
        \Foo\Bar\inner();
        \Foo\one_level_out();
        \top_level();
    }
}

outputs

Foo\Bar\Baz
Foo\Bar\Baz
Foo\Bar\Baz