Bug #76766 [Com]: Unexpected behavior, when using namespaces in one file

From: Date: Thu, 01 Dec 2022 08:25:53 +0000
Subject: Bug #76766 [Com]: Unexpected behavior, when using namespaces in one file
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-243011@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76766&edit=1 ID: 76766 Comment by: amin dot jabari242 at gmail dot com Reported by: dirk dot gerigk at atraveo dot com Summary: Unexpected behavior, when using namespaces in one file Status: Open Type: Bug Package: *Compile Issues Operating System: Linux PHP Version: 7.2.9 Block user comment: N Private report: N New Comment: I am still searching for some proper solution Same issue still no fix to this. (https://www.mycoverageinfo.biz/)github.com Previous Comments: ------------------------------------------------------------------------ [2018-08-21 21:50:23] cmb@php.net > That's *mostly* true. Indeed, there are some exceptions, but I would prefer to not overcomplicate the documentation, and also to leave some breathing room for future changes to the engine. After all, if code regards this simple rule, it should work in all circumstances (but see below). For instance, the first non-working example in the OP would have to be rewritten according to this rule to: namespace { new X\A; } namespace X { class B {} class A extends B {} } which would compile without errors[1]. > Classes must be declared before they are instantiated/subclassed. > http://www.php.net/language.oop5.inheritance Hmm, it seems the referenced man page does not say anything regarding instantiation, but rather speaks about extending classes and implementing interfaces, respectively. The former would only be relevant, if the class definition won't be resolved during compile time[2]. This is, however, basically the same for functions[3], so likely should be documented elsewhere (unless it already is). Not sure if the language specification[4] already addresses this. [1] <https://3v4l.org/UNvcS> [2] e.g. <https://3v4l.org/Xi4qg> [3] <https://3v4l.org/YSpRN> [4] <https://github.com/php/php-langspec> ------------------------------------------------------------------------ [2018-08-21 08:43:16] requinix@php.net > Classes must be declared before they are instantiated/subclassed. That's *mostly* true. More specifically, classes must be known to PHP before code tries to use them, however some class information is available after PHP initially parses a file and before it's done a more in-depth analysis. Defining classes in just about anything that uses {}s will disrupt that. https://3v4l.org/ZAEq1 But categorizing a namespace{} as one of the "anything that uses {}s" doesn't quite explain the reported behaviors. ------------------------------------------------------------------------ [2018-08-21 07:37:47] dirk dot gerigk at atraveo dot com But new A; class A {} should not work, because of 'Classes must be declared before they are instantiated/subclassed' And what is strange with namespaces that sometimes the behavior changes when interfaces are used, like this works namespace { new X\A; } namespace X { class B {} class A extends B {} } this not namespace { new X\A; } namespace X { interface T {} class B implements T {} class A extends B {} } In both examples the declaration is in the wrong order, but only the use of an interface breaks the code here. Whatever, i was only pointing to the strange behavior that makes it mostly impossible to merge complex namespaces in one file, when also a global namespace is present that instantiates objects. All examples here are simplified. How strange can it be when you are using classes that are extending and implementing from other namespaces. I came across it while i was merging code into one file. By the way i was just trying it for no real reason, just want to know if its possible. But thank for your comment ------------------------------------------------------------------------ [2018-08-20 23:13:53] a at b dot c dot de Your namespaces are not the problem: #THIS WORKS NOT class A extends B {} class B implements T {} interface T {} #THIS ALSO WORKS NOT interface T {} class A extends B {} class B implements T {} #BUT THIS WORKS class B implements T {} class A extends B {} interface T {} Or your last example: new A; class A {} Classes must be declared before they are instantiated/subclassed. http://www.php.net/language.oop5.inheritance I'm actually surprised that class B implements T {} class A extends B {} interface T {} works. ------------------------------------------------------------------------ [2018-08-20 07:37:11] dirk dot gerigk at atraveo dot com As not for the first tests, if no interface is used and nothing extends nothing, the order of the of declaration can be random. So this works also fine: namespace { new X\A; } namespace X { class A {} } Make your one combinations to see the strange behavior. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=76766 -- Edit this bug report at https://bugs.php.net/bug.php?id=76766&edit=1

« previous php.bugs (#243011) next »