Bu sınıf içinde, bir sınıfın örneklerini const diziler oluşturmak nasıl?

0 Cevap php

Ben kendi PHP sınıfı oluşturma. Ben bir numaralandırma gibi o sınıfın örnekleri bu sınıfın içinde sürekli referanslar, olmasını istediğiniz.

I keep getting 2 errors: 1. Constants cannot be arrays 2. parse error at line 11 (see below)

Ne oldu? Ben ciddi bir sabit dizi var değil mi? Ben bir Java kökenli değilim ...

Here is my code:

class Suit {
    const SUIT_NAMES = array("Club", "Diamond", "Heart", "Spade");
    const COLOURS = array("red", "black");

    const CLUB = new Suit("Club", "black");        // LINE 11
    const DIAMOND = new Suit("Diamond", "red");
    const HEART = new Suit("Heart", "red");
    const SPADE = new Suit("Spade", "black");

    var $colour = "";
    var $name = "";

    function __construct($name, $colour) {
        if (!in_array(self::SUIT_NAMES, $name)) {
            throw new Exception("Suit Exception: invalid suit name.");
        }
        if (!in_array(self::COLOURS, $colour)) {
            throw new Exception("Suit Exception: invalid colour.");
        }
        $this->name = $name;
        $this->colour = $colour;
    }
}

0 Cevap