Posted by Joe Scylla on 07/06/07 07:38
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($instance))
{
$instance = new User();
//some more code to create the object
}
return $instance;
}
</code>
[Back to original message]
|