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


Groups > comp.lang.php > #14537

Re: OO PHP result of new if a constructor fails

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: OO PHP result of new if a constructor fails
Date 2014-11-11 12:52 -0500
Organization A noiseless patient Spider
Message-ID <m3tidf$7h2$1@dont-email.me> (permalink)
References <m3t91i$rp6$1@dont-email.me> <m3t9et$t8s$1@dont-email.me> <m3tbjo$78r$1@dont-email.me> <m3tc0l$8ic$1@dont-email.me> <m3tdce$fd0$1@dont-email.me>

Show all headers | View raw


On 11/11/2014 11:27 AM, James Harris wrote:
> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message 
> news:m3tc0l$8ic$1@dont-email.me...
>> On 11/11/2014 10:56 AM, James Harris wrote:
>>> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
>>> news:m3t9et$t8s$1@dont-email.me...
>>>> On 11/11/2014 10:13 AM, James Harris wrote:
>>>>> From what I've found I understand that PHP constructors cannot return
>>>>> anything and that leads on to a specific query: if a piece of PHP code
>>>>> uses
>>>>> new as in
>>>>>
>>>>>   $a = new b();
>>>>>
>>>>> as long as no exception is thrown is $a guaranteed to hold something
>>>>> after
>>>>> the statement? Is it guaranteed to hold an object of the designated 
>>>>> type?
>>>>> What happens if b's constructor "fails" in some way for various 
>>>>> meanings
>>>>> of
>>>>> failure other than throwing exceptions?
>>>>>
>>>>> To put it another way, after the above instantiation statement which of
>>>>> these would be guaranteed to be true?
>>>>> 1) $a WILL contain a b object
>>>>> 2) $a could be NULL
>>>>> 3) $a might not exist (if it did not before the statement)
>>>>>
>>>>> Or does the answer depend on the specific class or on the version of 
>>>>> PHP?
>>>>>
>>>>> Sorry that's a lot of questions. Just trying to make the query clear.
>>>
>>> ...
>>>
>>>> The first question is - how did the constructor "fail"?
>>>
>>> Any way you can imagine without throwing an exception. I was hoping there
>>> was a universal rule that could apply to all of PHP OO programming.
>>>
>>
>> No, specifically - how can it fail?
>>
>>> If you mean that a constructor cannot fail I don't agree. If you mean 
>>> there
>>> are limited ways that a constructor can fail then I agree but for all and
>>> any of those means of failure what would be the effect on the object so
>>> constructed?
>>>
>>> If you want a specific, here's one.
>>>
>>>   $db1 = new mysqli(...);
>>>   if ($db1->connect_errno...)
>>>
>>> The instantiation can fail in a number of ways. Which of the scenarios
>>> labelled 1 to 3, above, will always be true when we reach the test
>>> statement? The test will baulk on two of them.
> 
> ...
> 
>> Specifically, how would the constructor fail?
>>
>> I'm not trying to give you a hard time.
> 
> Are you sure? .... :-(
> 
>>  You can't answer what happens
>> in case of failure without understanding the failure itself.
> 
> I think you may be talking about something else. I guess there are lots of 
> ways a constructor can fail: out of memory, no access to a resource, invalid 
> parameter value, incompatible parameters, disk IO error, network outage, 
> invalid credentials, faulty program etc etc. Without knowing the internals 
> of a particular constructor (not a good idea) your question about how that 
> constructor can fail cannot be answered. Even if it could the answer(s) 
> might change when a new version of that constructor is released. So there is 
> no way to sensibly enumerate all potential causes of failure and derive 
> findings from that.
> 
> So rather than asking about a specific constructor I was looking to see if 
> there was a PHP rule that could always be applied.
> 
> If that's not what you mean can you explain what you have in mind? I cannot 
> understand your meaning from what you have said so far.
> 
> James
> 
> 

As I said - it all depends on the mode of failure.  Things like a disk
IO error will cause a system error, and probably terminate the script
(depending on the error).  Faulty programming which causes a run-time
error will terminate the script.

Things like invalid credentials are not a constructor failure.  They may
be a failure to access a resource, but that's up to the constructor to
determine whether it is to be considered a non-recoverable error.  If it
is, the constructor should throw an exception.

As I said - it all depends on the cause of the error.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

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