Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.php Subject: Re: OO PHP result of new if a constructor fails Date: Wed, 12 Nov 2014 21:30:31 +0100 Organization: PointedEars Software (PES) Lines: 92 Message-ID: <2544086.7FOxZRQfaF@PointedEars.de> References: <4146385.JyWBYEY6r6@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1415824242 10066 eJwNycERwDAIA7CV6gNMGIdQvP8Irb4KIzjpDHoo9LpUVvbMwUWpOhk7/i/m5BraaiDnXWb2BxgzEOk= (12 Nov 2014 20:30:42 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Wed, 12 Nov 2014 20:30:42 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwFwQERADEIAzBL3Qp9kMPB8C/hE6eO+jO5zNc3E2TEckFc2PNKZsY1Vteih/Np/T4zKLspjh3FvtNT+AEnuRTV Cancel-Lock: sha1:PgxadHElv4jq0vzZpe9SHliphyI= X-NNTP-Posting-Host: eJwNx8kRwCAMBLCWDOzhlMMsof8SEv3EpaEYosDLCxWD1YV1MrFVA9u3M1UOH5/XaXun+bc/AKgQUw== Xref: csiph.com comp.lang.php:14550 Christoph M. Becker wrote: > Thomas 'PointedEars' Lahn wrote: >> James Harris wrote: >>> I understand that PHP constructors cannot return anything >> Doubtful. > > AIUI, a PHP constructor is the method __construct, […] A common misconception (which was discussed during the ZCE PHP 5.4 Exam Preparation Course because it is a common pitfall in the exam). Before there was the magic __construct() method (introduced with PHP 5.0), constructors could only be defined by a method whose identifier was the same as the class name, with the added catch that the matching was case- insensitive (as with all PHP functions). For backwards compatibility, this is still supported in PHP 5.x (just tested with PHP 5.6.2-1 CLI on Debian GNU/Linux); however, the __construct() method takes precedence for non-namespaced classes, and with namespaced classes the old behavior is no longer supported since PHP 5.3.3 (I, too, was at least not consciously aware of the latter – you can never learn enough). Ad-hoc test code to play with: // namespace N; class C { function __construct () { echo 42 . "\n"; } function C () { echo 23 . "\n"; } } new C(); You should observe that * in PHP 5.x, - “23” is printed if you comment out the __construct() definition and leave the namespace declaration commented out; - *if the PHP version is greater than or equal to 5.3.3*, nothing is printed if you comment out the __construct() definition *and* use the namespace declaration; - “42” is printed otherwise; * in PHP 4.x (with the namespace declaration commented out, because it is a syntax error there), - “23” should be printed if you do not comment out the X() definition; - nothing should be printed otherwise. > and that does not create an object, but merely initializes it, so the > constructor can be thought of as a command (opposed to a query), and as > such would never return anything (at least when properly called within a > /new/ statement). Since PHP 5.3.x at the latest, it is no longer possible to call constructors statically except with parent::…() [1], as that results in “Fatal error: Non-static method … cannot be called statically …” − lead me to find it in “zend_vm_execute.h” of PHP 5.3, but not PHP 5.2: Also, it would appear that when a method is called as a constructor (with the “new” keyword; in the aforementioned PHP 5.6 version – may also be different in previous versions), its return value is ignored even if it is a reference to an object. [Because of that, a constructor that can also serve as a factory, as in ECMAScript, for example, appears to be impossible in PHP.] -- PointedEars Zend Certified PHP Engineer Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.