Click to See Complete Forum and Search --> : Syntax Woes
code?
August 12th, 2009, 04:45 PM
Okay, so I can't do this?
<?php
class A { public function A() {} public function B() {} }
new A()->B();
?>
How can I do this in one line?
Also, what the difference between using __construct, and a function which has the same name as the class?
PeejAvery
August 12th, 2009, 06:04 PM
You can't do it in one line. First you have to create the object. Then after the object is created, then you may reference other methods contained within.
As for your second questions...it was just asked recent in this very same forum (http://www.codeguru.com/forum/showthread.php?t=481048).
bubu
August 18th, 2009, 07:20 PM
You can achieve something similar with a static constructor:
class A {
public static create()
{
return new A();
}
public function foo()
{
doSomething();
return $this;
}
public function bar()
{
$result = doOtherSomething();
return $result;
}
}
$barResult = A::create()->foo()->bar();
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.