I have a method in a class trying to return a pointer:
<?php
public function prepare( $query ) {
// bla bla bla
return &$this->statement;
}
?>
But it produces the following error:
Parse error: syntax error, unexpected '&' in /home/realst34/public_html/s98_fw/classes/sql.php on line 246
This code, however, works:
<?php
public function prepare( $query ) {
// bla bla bla
$statement = &$this->statement;
return $statement;
}
?>
Is this just the nature of PHP or am I doing something wrong?
PHP doesn't have pointers. PHP has reference. A reference in PHP is a true alias. But you don'tneed them. Objects are passed by handle (so they feel like references) for other data PHP uses a copy-on-write mechanism so there is no memory/performance penalty. The performance penalty comes when using references as they disable copy-on-write and therefore all the optimizations in the engine.
If you really want to return a reference youhave todeclare it in the signature:
public function &prepare( $query ) {
// bla bla bla
return $this->statement;
}
But as said: If $this->statement is an object there is no need.
See also http://php.net/references
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With