Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #14550
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: OO PHP result of new if a constructor fails |
| Date | 2014-11-12 21:30 +0100 |
| Organization | PointedEars Software (PES) |
| Message-ID | <2544086.7FOxZRQfaF@PointedEars.de> (permalink) |
| References | <m3t91i$rp6$1@dont-email.me> <4146385.JyWBYEY6r6@PointedEars.de> <m3uevr$s4b$1@solani.org> |
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).
<http://php.net/manual/en/language.oop5.decon.php>
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 …” −
<http://stackoverflow.com/a/19303354/855543>
lead me to find it in “zend_vm_execute.h” of PHP 5.3, but not PHP 5.2:
<http://lxr.php.net/search?q=cannot+be+called+statically&defs=&refs=&path=%2FPHP_5_3%2F&hist=&project=PHP_5_3>
<http://lxr.php.net/search?q=cannot+be+called+statically&defs=&refs=&path=%2FPHP_5_2%2F&hist=&project=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.
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
OO PHP result of new if a constructor fails "James Harris" <james.harris.1@gmail.com> - 2014-11-11 15:13 +0000
Re: OO PHP result of new if a constructor fails Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-11 10:20 -0500
Re: OO PHP result of new if a constructor fails "James Harris" <james.harris.1@gmail.com> - 2014-11-11 15:56 +0000
Re: OO PHP result of new if a constructor fails Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-11 11:03 -0500
Re: OO PHP result of new if a constructor fails "James Harris" <james.harris.1@gmail.com> - 2014-11-11 16:27 +0000
Re: OO PHP result of new if a constructor fails Matthew Carter <m@ahungry.com> - 2014-11-11 12:01 -0500
Re: OO PHP result of new if a constructor fails Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-11 12:56 -0500
Re: OO PHP result of new if a constructor fails Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-11 12:52 -0500
Re: OO PHP result of new if a constructor fails "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-11 18:12 +0100
Re: OO PHP result of new if a constructor fails Richard Damon <Richard@Damon-Family.org> - 2014-11-11 13:03 -0500
Re: OO PHP result of new if a constructor fails Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-11 23:54 +0100
Re: OO PHP result of new if a constructor fails "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-12 03:00 +0100
Re: OO PHP result of new if a constructor fails Matthew Carter <m@ahungry.com> - 2014-11-11 22:25 -0500
Re: OO PHP result of new if a constructor fails Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-11 22:36 -0500
Re: OO PHP result of new if a constructor fails Matthew Carter <m@ahungry.com> - 2014-11-12 01:30 -0500
Re: OO PHP result of new if a constructor fails Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-12 07:08 -0500
Re: OO PHP result of new if a constructor fails "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-12 13:30 +0100
Re: OO PHP result of new if a constructor fails Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-12 13:52 +0000
Re: OO PHP result of new if a constructor fails "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-13 00:46 +0100
Re: OO PHP result of new if a constructor fails Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-12 22:28 -0500
Re: OO PHP result of new if a constructor fails Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-12 21:30 +0100
Re: OO PHP result of new if a constructor fails "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-13 01:15 +0100
Re: OO PHP result of new if a constructor fails Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-13 04:09 +0100
Re: OO PHP result of new if a constructor fails Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2014-11-13 07:42 +0000
Re: OO PHP result of new if a constructor fails "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-13 14:11 +0100
Re: OO PHP result of new if a constructor fails Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-13 21:40 +0100
Re: OO PHP result of new if a constructor fails "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-14 13:49 +0100
Re: OO PHP result of new if a constructor fails Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-14 23:25 +0100
Re: OO PHP result of new if a constructor fails Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-12 22:29 -0500
Re: OO PHP result of new if a constructor fails Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-13 21:45 +0100
Re: OO PHP result of new if a constructor fails Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-13 15:56 -0500
Re: OO PHP result of new if a constructor fails Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-13 22:09 +0100
Re: OO PHP result of new if a constructor fails Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-13 21:03 -0500
Re: OO PHP result of new if a constructor fails "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-13 22:47 +0100
Re: OO PHP result of new if a constructor fails Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-13 22:59 +0100
Re: OO PHP result of new if a constructor fails "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-18 19:02 +0100
csiph-web