Can't do: $p = new Object()->method()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
will_
Forum Newbie
Posts: 4
Joined: Tue Oct 28, 2008 7:26 pm

Can't do: $p = new Object()->method()

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can't do: $p = new Object()->method()

Post 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.
will_
Forum Newbie
Posts: 4
Joined: Tue Oct 28, 2008 7:26 pm

Re: Can't do: $p = new Object()->method()

Post by will_ »

I tried that, too, but it still gave the same error. Thanks for the suggestion, though, tasairis.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Can't do: $p = new Object()->method()

Post 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);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Can't do: $p = new Object()->method()

Post 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.
will_
Forum Newbie
Posts: 4
Joined: Tue Oct 28, 2008 7:26 pm

Re: Can't do: $p = new Object()->method()

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Can't do: $p = new Object()->method()

Post by onion2k »

Can't you use the precision to differentiate between grid references (always ints?) and long/lat (always floats)?
will_
Forum Newbie
Posts: 4
Joined: Tue Oct 28, 2008 7:26 pm

Re: Can't do: $p = new Object()->method()

Post 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.
Post Reply