PHP - Does it ignore return val from constructors?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thecrow

    PHP - Does it ignore return val from constructors?

    I am trying to figure out how to make an object creation fail for ease
    of error handling. Oddly, I can't work out how to do it.

    Here's a reduced code sample of how I hoped it would work, but it
    doesn't:

    class Thing {
    var $error;
    function Thing() {
    return 0;
    }
    }

    if (!$theThing = new Thing()) {
    echo "Couldn't create the thing -" . $thing->error;
    }

    I guess I could set $this to false instead of returning 0. But then I
    wouldn't get the benefit of the error message, unless I echoed it,
    which I don't like to do in classes.

    Any good way to do this?

  • Marcin Dobrucki

    #2
    Re: PHP - Does it ignore return val from constructors?

    thecrow wrote:[color=blue]
    > I am trying to figure out how to make an object creation fail for ease
    > of error handling. Oddly, I can't work out how to do it.
    >
    > Here's a reduced code sample of how I hoped it would work, but it
    > doesn't:
    >
    > class Thing {
    > var $error;
    > function Thing() {
    > return 0;
    > }
    > }
    >
    > if (!$theThing = new Thing()) {
    > echo "Couldn't create the thing -" . $thing->error;
    > }
    >
    > I guess I could set $this to false instead of returning 0. But then I
    > wouldn't get the benefit of the error message, unless I echoed it,
    > which I don't like to do in classes.[/color]

    I have a recollection that you can't get return values from
    constructors. The way around it would be something like this:

    class Foo {

    function Foo () {
    $this->initialize() ;
    }

    function initialize() {
    if (yadayadayada) {
    ...
    }
    else {
    return 0;
    }
    }
    }

    /Marcin

    Comment

    • Chung Leong

      #3
      Re: PHP - Does it ignore return val from constructors?

      "thecrow" <carltonbrown@h otmail.com> wrote in message
      news:1109247316 .134425.288660@ z14g2000cwz.goo glegroups.com.. .[color=blue]
      > I am trying to figure out how to make an object creation fail for ease
      > of error handling. Oddly, I can't work out how to do it.
      >
      > Here's a reduced code sample of how I hoped it would work, but it
      > doesn't:
      >
      > class Thing {
      > var $error;
      > function Thing() {
      > return 0;
      > }
      > }
      >
      > if (!$theThing = new Thing()) {
      > echo "Couldn't create the thing -" . $thing->error;
      > }
      >
      > I guess I could set $this to false instead of returning 0. But then I
      > wouldn't get the benefit of the error message, unless I echoed it,
      > which I don't like to do in classes.
      >
      > Any good way to do this?
      >[/color]

      Doing what you're not supposed to:

      class Thing {
      var $error;
      function Thing() {
      $this = false;
      }
      }



      Comment

      • thecrow

        #4
        Re: PHP - Does it ignore return val from constructors?

        Thank you for the explanation.

        That leaves me with the problem, if I fail the constructor by setting
        $this to false, how do I get the error information without echoing it?

        I guess I can go search on that, but if anyone has any commonly used
        ideas I would appreciate it.

        Comment

        • Alessandro Ranellucci

          #5
          Re: PHP - Does it ignore return val from constructors?

          In article <1109294863.754 779.98710@g14g2 000cwa.googlegr oups.com>,
          "thecrow" <carltonbrown@h otmail.com> wrote:
          [color=blue]
          > Thank you for the explanation.
          >
          > That leaves me with the problem, if I fail the constructor by setting
          > $this to false, how do I get the error information without echoing it?
          >
          > I guess I can go search on that, but if anyone has any commonly used
          > ideas I would appreciate it.[/color]

          Use a manual constructor:

          class Thing {
          function Thing {
          #nothing here
          }
          function fetch {
          if ($CanInstantiat e) {
          return new Thing;
          } else {
          return false;
          }
          }
          }

          - Alessandro

          Comment

          • Chung Leong

            #6
            Re: PHP - Does it ignore return val from constructors?

            "thecrow" <carltonbrown@h otmail.com> wrote in message
            news:1109294863 .754779.98710@g 14g2000cwa.goog legroups.com...[color=blue]
            > Thank you for the explanation.
            >
            > That leaves me with the problem, if I fail the constructor by setting
            > $this to false, how do I get the error information without echoing it?
            >
            > I guess I can go search on that, but if anyone has any commonly used
            > ideas I would appreciate it.
            >[/color]

            That was just a stupid PHP 4 trick. Don't use it. It doesn't work in PHP 5.

            Constructors are meant for initializing an object. They are not supposed to
            fail. The only reasonable way to handle a failure in a constructor is
            through exception handling, which doesn't exist in PHP 4. As others have
            suggested, put the code that could potentially fail in a separate function.


            Comment

            Working...