4

PHP Factory パターンに関する Web 上の例を参照してください。

$kind =& Vehicle::category($wheel);では、なぜ使用する必要があります&か?

コード:

<?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

2 に答える 2

5

スタック オーバーフローの質問リファレンスから - この記号は PHP で何を意味しますか? :

=&参考文献

(削除された)コメントが言ったように、最後のリンクはあなたの特定のケースに当てはまるはずです。

于 2012-05-13T07:26:10.770 に答える
4

この例では定数への参照を取得しているため、ここでは役に立ちませんが、変更される可能性のある値を「監視」したい場合や、それに合わせて変数を変更したい場合には、参照が使用されます。

于 2012-05-13T07:23:10.463 に答える