473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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


Jul 6 '07 #1
4 2427
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>
Jul 6 '07 #2
Rik
On Fri, 06 Jul 2007 09:38:05 +0200, Joe Scylla <jo********@gmail.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($instance))
{
$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
Jul 6 '07 #3
Rik wrote:
On Fri, 06 Jul 2007 09:38:05 +0200, Joe Scylla <jo********@gmail.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($instance))
{
$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>
Jul 6 '07 #4
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
Jul 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jerrygarciuh | last post by:
Hello, Is it possible to instantiate a child class within the constructor of its parent? eg Class DBI extends DB { function DBI() { // explicit parent constructor call
2
by: Colin Mc Mahon | last post by:
Hi all, I currently use a class to interface with my databases, allowing me to insert, update, delete and retrieve records from the database as methods of the class. I have now created a...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
8
by: Carel Lotz | last post by:
H We have ported our VB 6 application into VB .NET but are still integrating with a few COM + applications written in VB6 running on our application server (Win 2000 Server). We have the proxies...
9
by: the_grove_man | last post by:
I guess my question can go in two directions. I create applications that run multiple queries against a database. Generally speaking in the past I have used a Data Control (calling it dat1)...
14
by: Ray5531 | last post by:
I have a console application in my local computer which I like to use remoting in it,to instanciate an object (MyClass.dll) in a web application(its bin folder) in a completely seperated box(in the...
6
by: Mariano | last post by:
Hi, I have stored a menu in a database. The menu is dynamically updated and last level menu items contain the object type that should be instantiated. I am looking for a way to instantiate the...
13
by: Brian | last post by:
I have many similar classes in a project, one for each type of report my app can create. I want to instantiate them based on a value passed in by a scheduler module. Right now I have Sub...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.