Level 75
Because collection is a class.
https://www.php.net/manual/en/language.oop5.references.php
$c2 = $this->es2(clone $c1);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I don't expect a php function to modify its parameters (not passed by reference)
Can you explain why es2 function below modify parameter c1 ?
function es1($p1) {
$p1++;
return $p1;
}
function es2($c1) {
$c1->pull(0);
$c1=$c1->values();
return $c1;
}
$p1=2;
echo "p1=$p1<br>";
$p2=$this->es1($p1);
echo "p1=$p1 p2=$p2<br>";
// p1=2
// p1=2 p2=3
$c1=collect([1,2]);
echo "c1=$c1<br>";
$c2=$this->es2($c1);
echo "c1=$c1 c2=$c2<br>";
// c1=[1,2]
// c1={"1":2} c2=[2]
Because collection is a class.
https://www.php.net/manual/en/language.oop5.references.php
$c2 = $this->es2(clone $c1);
Please or to participate in this conversation.