Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #14581

Re: OO PHP result of new if a constructor fails

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From "Christoph M. Becker" <cmbecker69@arcor.de>
Newsgroups comp.lang.php
Subject Re: OO PHP result of new if a constructor fails
Date Fri, 14 Nov 2014 13:49:51 +0100
Organization solani.org
Lines 70
Message-ID <m44tp7$9l0$1@solani.org> (permalink)
References <m3t91i$rp6$1@dont-email.me> <4146385.JyWBYEY6r6@PointedEars.de> <m3uevr$s4b$1@solani.org> <2544086.7FOxZRQfaF@PointedEars.de> <m40t74$pi6$1@solani.org> <2482034.fQki01Cq4G@PointedEars.de> <m42aln$t2l$1@solani.org> <14454844.m36nGyrlVr@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Trace solani.org 1415969383 9888 eJwFwQkBwDAIA0BL0EJI5YzPv4Td+YWiwuAwX998Mvr1yemAaRakCd1HQ3f6FZLlAzlngvwBJGkQ4A== (14 Nov 2014 12:49:43 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Fri, 14 Nov 2014 12:49:43 +0000 (UTC)
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0
X-User-ID eJwFwYEBACAEBMCVUP9qHMT+I3SHRWX5JrgxmJtsG7np0ivELVCOVo/Qc3I0Te7TZRXkIz4cURD1
In-Reply-To <14454844.m36nGyrlVr@PointedEars.de>
Cancel-Lock sha1:qTu1sX4TA22v25MdtDRDZhATtnU=
X-NNTP-Posting-Host eJwNwokRwDAIA7CVAgU7jMPX/UdoT/IHgqbBYf7+WrnTjb3UAotIlrod2YxIlaHk3cyQMyMfJKcRWg==
Xref csiph.com comp.lang.php:14581

Show key headers only | View raw


Thomas 'PointedEars' Lahn wrote:

> Christoph M. Becker wrote:
> 
>> Thomas 'PointedEars' Lahn wrote:
>>> Christoph M. Becker wrote:

>> The PHP developers seem to consider the specification authoritative, see
>> e.g. <http://news.php.net/php.internals/78871>f.
> 
> I do not see the connection.  Wrong ID?

The messages are part of a larger thread; unfortunately, the web
interface doesn't seem to allow viewing by thread, but rather all
threads are interleaved.  The following message
<http://news.php.net/php.internals/78872> (what I wanted to indicate
with the trailing "f"), makes it a bit more clear.  The "spec" they're
mentioning, is the lang-spec (not the PHP manual).

>>>> AFAIK, the behavior of ECMAScript is rather uncommon, but quite useful
>>>> (especially wrt. calling the constructor function without new), but can
>>>> be simulated in PHP with static factory methods and (a) private
>>>> constructor(s).
>>> No.
>>
>> Why not?
> 
> Basically, because you cannot do the following in PHP and have a *D* 
> instance referred by *both* ”o” *and* “o2”:
> 
>   function C ()
>   {
>     function D () {}
> 
>     return new D();
>   }
> 
>   var o = new C();
>   var o2 = C();
> 
> Your static factory method is a *different* function that you *cannot* 
> invoke with “new” as well.  Therefore, it is not a proper _emulation_.

Indeed, it is not a proper *emulation* (thanks for the correction).
However, you can do the following:

    class D {}

    class C {
        static function create() {
            return new D();
        }
        private function __construct() {}
    }

    $o = C::create();
    $o2 = C::create();

IMHO, that comes quite close to the intention of returning an instance
of another class, when trying to create a certain class' instance.  If
PHP had nested classes (a respective RFC[1] has been withdrawn, though),
it would even come closer.  (For now D could be regarded as been
private, and documented as such, and maybe renamed to _D.)  The
possibility of creating an instance with or without /new/, seems to be
more of a technical detail.

[1] <https://wiki.php.net/rfc/nested_classes>

-- 
Christoph M. Becker

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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