Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Matthew Carter Newsgroups: comp.lang.php Subject: Re: OO PHP result of new if a constructor fails Date: Wed, 12 Nov 2014 01:30:53 -0500 Organization: Ahungry (http://ahungry.com) Lines: 66 Message-ID: <87wq71ue02.fsf@ahungry.com> References: <4146385.JyWBYEY6r6@PointedEars.de> <871tp9w15b.fsf@ahungry.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="7c986cd4736462de309a749b207746fe"; logging-data="29959"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/u5WDnUPXTHrmTOes4i5p/" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:tsmLF04rLqBx8ss/s2gIYI9oVcQ= sha1:7a7E1wJw69M7HF65FInXD12AlQc= Xref: csiph.com comp.lang.php:14544 Jerry Stuckle writes: > On 11/11/2014 10:25 PM, Matthew Carter wrote: >> "Christoph M. Becker" writes: >> >>> Thomas 'PointedEars' Lahn wrote: >>> >>>> James Harris wrote: >>>> >>>>> I understand that PHP constructors cannot return anything >>>> >>>> Doubtful. >>> >>> AIUI, a PHP constructor is the method __construct, 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). >> >> Although you can return any value you like when an extending class calls >> it via a parent::__construct() call. >> > > Although PHP may not complain (I don't know, I've never tried it), > returning a value from a __construct() method is documented as invalid. > See http://php.net/manual/en/language.oop5.decon.php. The return value > of the function is void (no return value). So would this be a case of a bug in PHP or a bug in the documentation? __construct ()); Produces output (with all warnings turned on): int(42) int(42) Obviously magically calling/running the __construct via a new class instance disregards the return result though. -- Matthew Carter (m@ahungry.com) http://ahungry.com