Page 1 of 1
Can't do: $p = new Object()->method()
Posted: Tue Oct 28, 2008 7:33 pm
by will_
Hi,
I'm trying to do this:
Code: Select all
$pos = new Position()->setOSGrid(123,456);
where
Code: Select all
function setOSGrid($x, $y) {
(some other stuff)
return $this;
}
but I get
Code: Select all
syntax error, unexpected T_OBJECT_OPERATOR
I tried putting brackets around (new Position()->setOSGrid()) but it didn't help. I know I could do
Code: Select all
$pos = new Position();
$pos->setOSGrid(123,456)
but I rather liked the "inline" way I had. Is it possible?
Thanks.
Re: Can't do: $p = new Object()->method()
Posted: Tue Oct 28, 2008 7:59 pm
by requinix
How about putting those parentheses around something else? Like
Code: Select all
($pos = new Position())->setOSGrid(123, 456);
If that doesn't work then you simply can't do it.
Re: Can't do: $p = new Object()->method()
Posted: Tue Oct 28, 2008 8:05 pm
by will_
I tried that, too, but it still gave the same error. Thanks for the suggestion, though, tasairis.
Re: Can't do: $p = new Object()->method()
Posted: Wed Oct 29, 2008 1:56 pm
by pickle
The only way you can do that is if setOSGrid() doesn't reference any variables in it's parent object. Then you could do:
Code: Select all
$pos = Position::setOSGrid(123,456);
Otherwise, you're stuck with:
Code: Select all
$Position = new Position();
$pos = $Position->setOSGrid(123,456);
Re: Can't do: $p = new Object()->method()
Posted: Wed Oct 29, 2008 2:18 pm
by onion2k
Could you modify the Position class?
Code: Select all
class Position {
public function __construct($x=0, $y=0) {
if (!empty($x) and !empty($y)) { $this->setOSGrid($x,$y); }
}
public function setOSGrid($x, $y) {
//Whatever
}
}
Then use $pos = new Position(123,456) to instantiate the object.
EDIT: Probably not with empty() mind you, otherwise you wouldn't be able to set the position to 0,0.
Re: Can't do: $p = new Object()->method()
Posted: Wed Oct 29, 2008 2:34 pm
by will_
Thanks, onion and pickle, for your suggestions. (All we need now for a sandwich is some cheese.)
The problem I have is that I might want to initialise the Position with an Ordnance Survey (OS) grid reference or with a latitude/longitude. For example, $pos1 and $pos2 are both centred on London:
Code: Select all
$pos1 = new Position();
$pos1->setOSGrid(530, 180);
$pos2 = new Position();
$pos2->setLatLong(51.5, -0.126);
$grid1 = $pos1->getOSGrid(); // returns array(530, 180);
$grid2 = $pos2->getOSGrid(); // returns array(530, 180);
I suppose I could modify the constructor further to include an indication of the type of data being supplied:
Code: Select all
function __construct($type, $eastlat, $northlong)
...
and then
Code: Select all
$pos1 = new Position(Position::POSITION_TYPE_OSGRID, 530, 180);
$pos2 = new Position(Position::POSITION_TYPE_LATLONG, 51.5, -0.126);
$grid1 = $pos1->getOSGrid(); // returns array(530, 180);
$grid2 = $pos2->getOSGrid(); // returns array(530, 180);
I suppose that would do, it's just not quite as neat as I'd hoped for.
Re: Can't do: $p = new Object()->method()
Posted: Wed Oct 29, 2008 3:22 pm
by onion2k
Can't you use the precision to differentiate between grid references (always ints?) and long/lat (always floats)?
Re: Can't do: $p = new Object()->method()
Posted: Wed Oct 29, 2008 3:40 pm
by will_
Can't you use the precision to differentiate between grid references (always ints?) and long/lat (always floats)?
Thanks for the suggestion but the system I'm using could use floating point OS grid references to refer to locations with a precision of better than a kilometer. There's also no reason why a latitude or longitude couldn't be an integer.