I'm a intermediate C++ programmer and know that you can pass a constant reference as a parameter in order to prevent editing to the actual variable. I was wondering if I could do this in PHP?
Asked
Active
Viewed 4,430 times
4
-
Your title is confusing because PHP has class constants. – Roger Halliburton Jan 22 '11 at 06:46
-
The title seems OK, however as I did not find any equivalent, this answer suggest to clone the object, and THEN, pass it to a function - http://stackoverflow.com/a/11368155/1835470 , but clone makes just a shallow copy... http://php.net/manual/en/language.oop5.cloning.php – jave.web Apr 15 '15 at 20:22
3 Answers
7
No, there is no equivalent to C++'s const
qualifier in PHP.

Jacob Relkin
- 161,348
- 33
- 346
- 320
-
If he's coming from c++(where you can't set objects to const), he's not going to be trying to do that. – ehudokai Jan 22 '11 at 04:09
-
Objects aren't technically passed by reference by default. The location/handle of the object is passed by value. So yes, you can modify the object by calling its methods, but you cannot actually change what the calling scope points to in the default case. All that said, PHP doesn't really have an equivalent to C++'s `const`. – Matthew Jan 22 '11 at 04:11
-
After more than 4 years later and also after the feature freeze of PHP 7 (including scalar type hints), an equivalent for C++'s `const` qualifier seems not to be added soon. – AbcAeffchen May 02 '15 at 11:46
2
Is this what you're talking about:
<?php
$a = 10;
function foo($p_a) {
// passing by value is the default
$p_a++;
}
foo($a);
echo $a; // prints 10
$a = 10;
function bar(&$p_a) {
//-------^ passing by reference
$p_a++;
}
bar($a);
echo $a; // prints 11
?>

Salman A
- 262,204
- 82
- 430
- 521
-
see my answer below; your case does hold for scalars; I'm adding my answer only for completeness, for anyone who end up here searching for the same topic. – ϹοδεMεδιϲ Jul 11 '11 at 17:24
0
@Salman A, that works only for scalar, the behaviour is different for objects when they are passed by reference or not. It looks like there is no real difference between the two methods!
<?php
class X
{
static $instances = 0;
public $instance;
public $str;
function __construct($str)
{
$this->instance = ++self::$instances;
$this->str = $str;
}
public function __toString()
{
return "instance: ".$this->instance." str: ".$this->str;
}
}
class Y extends X
{
public function __toString()
{
return "Y:".parent::__toString();
}
}
// Pass NORMAL
function modify_1($o)
{
$o->str = __FUNCTION__;
}
// Pass BY-REFERENCE
function modify_2(&$o)
{
$o->str = __FUNCTION__;
}
// Pass NORMAL - Obj Replace
function modify_3($o)
{
$o = new Y(__FUNCTION__);
}
// Pass BY-REFERENCE - Obj Replace
function modify_4(&$o)
{
$o = new Y(__FUNCTION__);
}
$x = new X('main');
echo "$x\n";
modify_1($x);
echo "$x\n";
modify_2($x);
echo "$x\n";
modify_3($x);
echo "$x\n";
modify_4($x);
echo "$x\n";
That generated the below output;
instance: 1 str: main
instance: 1 str: modify_1
instance: 1 str: modify_2
instance: 1 str: modify_2
Y:instance: 3 str: modify_4
I was expecting
instance: 1 str: main
instance: 1 str: main
instance: 1 str: modify_2
instance: 1 str: modify_2
Y:instance: 3 str: modify_4
So my conclusion is; it does seem to work if we are dealing with object ( itself ) or a scalar; but not an object's property or its method.

ϹοδεMεδιϲ
- 2,790
- 3
- 33
- 54