Reply to Re: Can I instantiate a new object within an if statement?

Your name:

Reply:


Posted by Rik on 07/06/07 10:37

On Fri, 06 Jul 2007 09:38:05 +0200, Joe Scylla <joe.scylla@gmail.com> =

wrote:

> Phil Latio wrote:
>> Here is some basic example code:
>> $newUser =3D new User();
>> if ($newUser->SaveNewUser())
>> {
>> echo "success";
>> }
>> else
>> {
>> echo "failure";
>> }
>> Question. Can I modify the above so I instantiate a new object withi=
n =

>> 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 =3D null;
> if (is_null($instance))
> {
> $instance =3D 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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация