Can I instantiate a new object within an if statement?

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

    Can I instantiate a new object within an if statement?

    Here is some basic example code:

    $newUser = new User();
    if ($newUser->SaveNewUser( ))
    {
    echo "success";
    }
    else
    {
    echo "failure";
    }

    Question. Can I modify the above so I instantiate a new object within an if
    statement and apply a method all on one line?

    Cheers

    Phil




  • Joe Scylla

    #2
    Re: Can I instantiate a new object within an if statement?

    Phil Latio wrote:
    Here is some basic example code:
    >
    $newUser = new User();
    if ($newUser->SaveNewUser( ))
    {
    echo "success";
    }
    else
    {
    echo "failure";
    }
    >
    Question. Can I modify the above so I instantiate a new object within an if
    statement and apply a method all on one line?
    >
    Cheers
    >
    Phil
    Not if you want to use the *new* statement.

    But you can add a static method to the class returning the object, so
    you can use following code:

    <code>
    if (User::instance ()->saveNewUser( ))
    {
    echo "success";
    }
    else
    {
    echo "failed";
    }
    </code>

    the static method may look like this:

    <code>
    public static function &instance()
    {
    static $instance = null;
    if (is_null($insta nce))
    {
    $instance = new User();
    //some more code to create the object
    }
    return $instance;
    }
    </code>

    Comment

    • Rik

      #3
      Re: Can I instantiate a new object within an if statement?

      On Fri, 06 Jul 2007 09:38:05 +0200, Joe Scylla <joe.scylla@gma il.com
      wrote:
      Phil Latio wrote:
      >Here is some basic example code:
      > $newUser = new User();
      >if ($newUser->SaveNewUser( ))
      >{
      > echo "success";
      >}
      >else
      >{
      > echo "failure";
      >}
      > Question. Can I modify the above so I instantiate a new object within
      >an if statement and apply a method all on one line?
      > Cheers
      > Phil
      >
      Not if you want to use the *new* statement.
      >
      But you can add a static method to the class returning the object, so
      you can use following code:
      >
      <code>
      if (User::instance ()->saveNewUser( ))
      {
      echo "success";
      }
      else
      {
      echo "failed";
      }
      </code>
      >
      the static method may look like this:
      >
      <code>
      public static function &instance()
      {
      static $instance = null;
      if (is_null($insta nce))
      {
      $instance = new User();
      //some more code to create the object
      }
      return $instance;
      }
      </code>
      While this can certainly be done, this is a way to create a Singleton.
      Unless that's what the OP wants, forget about the static $instance.


      --
      Rik Wasmus

      Comment

      • Joe Scylla

        #4
        Re: Can I instantiate a new object within an if statement?

        Rik wrote:
        On Fri, 06 Jul 2007 09:38:05 +0200, Joe Scylla <joe.scylla@gma il.com>
        wrote:
        >
        >Phil Latio wrote:
        >>Here is some basic example code:
        >> $newUser = new User();
        >>if ($newUser->SaveNewUser( ))
        >>{
        >> echo "success";
        >>}
        >>else
        >>{
        >> echo "failure";
        >>}
        >> Question. Can I modify the above so I instantiate a new object
        >>within an if statement and apply a method all on one line?
        >> Cheers
        >> Phil
        >>
        >Not if you want to use the *new* statement.
        >>
        >But you can add a static method to the class returning the object, so
        >you can use following code:
        >>
        ><code>
        >if (User::instance ()->saveNewUser( ))
        > {
        > echo "success";
        > }
        >else
        > {
        > echo "failed";
        > }
        ></code>
        >>
        >the static method may look like this:
        >>
        ><code>
        >public static function &instance()
        > {
        > static $instance = null;
        > if (is_null($insta nce))
        > {
        > $instance = new User();
        > //some more code to create the object
        > }
        > return $instance;
        > }
        ></code>
        >
        While this can certainly be done, this is a way to create a Singleton.
        Unless that's what the OP wants, forget about the static $instance.
        >
        You are right. The instance method dont have to be a static:

        <code>
        //class
        class test
        {
        private $Id = 0;
        public function __construct($id = 0)
        {
        $this->Id = $id;
        }
        public function instance($id)
        {
        $r = new test($id);
        //do some more stuff
        return $r;
        }
        public function getId()
        {
        return ($this->Id 0) ? $this->Id : false;
        }
        }

        //test
        if (test::instance (1)->getId())
        {
        echo "success";
        }
        else
        {
        echo "failed";
        }
        </code>


        Comment

        • gosha bine

          #5
          Re: Can I instantiate a new object within an if statement?

          On 06.07.2007 08:27 Phil Latio wrote:
          Here is some basic example code:
          >
          $newUser = new User();
          if ($newUser->SaveNewUser( ))
          {
          echo "success";
          }
          else
          {
          echo "failure";
          }
          >
          Question. Can I modify the above so I instantiate a new object within an if
          statement and apply a method all on one line?
          >
          Cheers
          >
          Phil
          >
          >
          >
          >
          No, this is a limitation of php parser

          (new Object)->method()

          is not possible. Don't ask why. ;)


          --
          gosha bine

          extended php parser ~ http://code.google.com/p/pihipi
          blok ~ http://www.tagarga.com/blok

          Comment

          Working...