By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

$class->method()->method()

P: n/a
edg
This works:

$b = $a->method1();
$c = $b->method2();

I'd like to get this all on one expression, something like:

$c = ${$a->method1()}->method2();
which doesn't work.

Is there any way to avoid actually creating and then using the $b
object?

Thanks,
</edg>
Ed Greenberg
San Jose CA USA

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ed*@greenberg.org wrote:
This works:

$b = $a->method1();
$c = $b->method2();

I'd like to get this all on one expression, something like:

$c = ${$a->method1()}->method2();
which doesn't work.

Is there any way to avoid actually creating and then using the $b
object?

Thanks,
</edg>
Ed Greenberg
San Jose CA USA


If I'm not mistaking you'll be needing PHP5 to use that syntax...
In PHP5 $a->method1()->method2() works, in PHP4 it doesn't as far as I know.

Rutger
--
Rutger Claes rg*@rgc.tld
Replace tld with top level domain of belgium to contact me pgp:0x3B7D6BD6
Do not reply to the from address. It's read by /dev/null and sa-learn only

Jul 17 '05 #2

P: n/a
ed*@greenberg.org wrote:
This works:

$b = $a->method1();
$c = $b->method2();

I'd like to get this all on one expression, something like:

$c = ${$a->method1()}->method2();
which doesn't work.


Use $c = $a->method1()->method2;

You'll need PHP 5 for this syntax.

Regards,
Matthias
Jul 17 '05 #3

P: n/a
On 6 Jan 2005 09:41:24 -0800, ed*@greenberg.org wrote:
This works:

$b = $a->method1();
$c = $b->method2();

I'd like to get this all on one expression, something like:

$c = ${$a->method1()}->method2();
which doesn't work.

Is there any way to avoid actually creating and then using the $b
object?


andyh@server:~/public_html$ cat test.php
<?php
class B {
function method2()
{
return 'x';
}
}

class A {
function method1()
{
return new B();
}
}

$a = new A();
$c = $a->method1()->method2();

print $c;
print "\n";
?>

andyh@server:~/public_html$ php -q test.php
x

andyh@server:~/public_html$ php -v
PHP 5.0.3 (cli) (built: Dec 22 2004 02:41:40)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.3, Copyright (c) 1998-2004 Zend Technologies

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #4

P: n/a
edg
Thanks for all the responses.

I suppose I should have posted that I have to use PHP4 :(
Any ideas for the more traditional approach?

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.