Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I need "&"?

Tags:

oop

php

See example on the web about the PHP Factory Pattern.

In the line $kind =& Vehicle::category($wheel);, why should I to 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());
?>
like image 418
israel love php Avatar asked Jan 16 '23 16:01

israel love php


1 Answers

From Stack Overflow question Reference - What does this symbol mean in PHP?:

=& References

  • Reference assignment operator in PHP, =&
  • What do the "=&" and "&=" operators in PHP mean?
  • What do the '&=' and '=&' operators do?
  • What does =& mean in PHP?

As the (deleted) comment said, the last link should apply to your specific case.

like image 156
dukevin Avatar answered Jan 25 '23 16:01

dukevin