PHP: this is $ this-> something & # 8594; ($ this-> foo) & # 8594; bar legal?

Is this line legal PHP?

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

if $ this-> db is a constant using the name db.

thank

+5
source share
5 answers

No, strings (and therefore your constant) must be enclosed in brackets, for example:

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

Try using curly braces instead of parentheses:

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

Or by assigning to the $this->dblocal var and using this instead:

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

$connection->db->collection :

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

. +1 BoltClock , $x->y->z.

+1

, → () → mongo → ($ this- > db) → $coll... ,

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

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

0

All Articles