开发者

PHP: is $this->something->($this->foo)->bar legal?

Is this line legal PHP?

$this->mongo->($this->db)->$collection_name->insert($document_name);

if $this->db is a constant with the db name to use.开发者_开发百科

Thank you


Try using curly braces instead of parentheses:

$this->mongo->{$this->db}->$collection_name->insert($document_name);

Or assigning $this->db to a local var and using that instead:

$db_name = $this->db;
$this->mongo->$db_name->$collection_name->insert($document_name);


No, strings (and thus your constant) should be wrapped in brackets, like this:

$this->mongo->{$this->db}->$collection_name->insert($document_name);


$connection->db->collection is just shorthand for:

$this->mongo->selectDB($this->db)->selectCollection($collection_name)->insert(...);

which might work better in your case. +1 for BoltClock's answer, too, though, if you want stick with the $x->y->z style.


No, you can not have ->()-> where you have mongo->($this->db)->$coll... Maybe you were looking for

$this->mongo($this->db)->$collection_name->insert($document_name);


You wanted $this->mongo->selectDB($this->db)->$collection_name->insert($document_name)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜