Why do I need "&"?

See an example on the web about the PHP Factory pattern.

In the line $kind =& Vehicle::category($wheel); why should i use & ?

The code:

 <?php class Vehicle { function category($wheel = 0) { if ($wheel == 2) { return "Motor"; } elseif($wheel == 4) { return "Car"; } } } class Spec { var $wheel = ''; function Spec($wheel) { $this->wheel = $wheel; } function name() { $wheel = $this->wheel; return Vehicle::category($wheel); } function brand() { $wheel = $this->wheel; $kind =& Vehicle::category($wheel); if ($kind == "Motor") { return array('Harley','Honda'); } elseif($kind = "Car") { return array('Nisan','Opel'); } } } $kind = new Spec(2); echo "Kind: ".$kind->name(); echo "<br>"; echo "Brand: " . implode(",", $kind->brand()); ?> 
+4
source share
2 answers

It is useless here, because the example gets a reference to a constant, but references have their own capabilities when you want to "look" at a value that can change, and you want your variable to change with it.

+4
source
+5
source

Source: https://habr.com/ru/post/1412225/


All Articles