Hi,

I'm having some problems with objects, could someone just confirm if this is possible or if I'm fighting a loosing battle, this code isn't my actual code it's just a representation of what I want to achieve, basically I want to be able to return false from the default constructor of a class if an action fails, the if() always seems to evalute to true:


class someClass {

  var myVar;

  function someClass() {

$this->myVar = 'test'

if($a = 1) return true;
else return false;
  }

}

$newObject = new someClass();
if($newObject) echo 'true';
else echo 'false';

Thanks in advance, i've been staring at it too long.

Andrew

    I made the same mistake in one of my posts..
    Type $a == 1 in your if statement. So with a double =-sign.

      Sorry,

      That was a typo when I typed the post - Any other suggestions?

        Does this make a difference?

        if($a == 1)
        {
        return true;
        }
        else 
        {
        return false;
        }

          No difference.

          The problem is that myObject is always true when you instantiate the object, I'm looking for a way that I can (during the constructor) bail back to the instantiation and say 'look it didn't work out' ie FALSE;

          if($myObject = new myClass()) {

          echo 'true';
          else {

          echo 'false';
          }

          Does this make things clearer?

            Well, I would think that it is possible to return values from a constructor, though it probably isnt good practice.

            An alternative might be to have a $error member variable which is set to true if the constructor fails.

              A couple of user notes in the manual offer a couple of suggestions:

              steffen staehle
              17-Jan-2003 09:11

              A note on a constructor's return value:

              When creating an instance of a class, 'new' obviously returns the object, not the constructor's return value (if any).

              Still, it might be tempting to communicate the success or failure of constructing the object via a return value:

              class A
              {
                function A()
                {
                    // ...
                    // some error occurred
                    return FALSE;   
              } } if ($a = new A()) { // success, use $a ... }

              THIS WILL NOT WORK the way intended.
              'new' returns the object, the assignment returns the value of the assignment, i.e. the object, and whether an object evaluates as TRUE does not depend on the constructor's return value.

              My preferred solution - set some object property during the construction phase that you can check against explicitely.

              ogarbe at tf1 dot fr
              01-Apr-2003 12:01

              to steffen staehle

              If you want your constructor to possibly not create the object

              class A
              {
               function A()
               {
                   // ...
                   // some error occurred
                   $this = null;
                  return;   
              } } if ($a = new A()) { // success, use $a ... }

              note that null==false.

                Write a Reply...