如何避免在php中使用数组作为类常量?

时间:2020-12-08 10:55:53

I have a class with a static method. There is an array to check that a string argument passed is a member of a set. But, with the static method, I can't reference the class property in an uninstantiated class, nor can I have an array as a class constant.

我有一个静态方法的类。有一个数组来检查传递的字符串参数是否为集合的成员,但是,使用静态方法,我不能引用未实例化类中的类属性,也不能使用数组作为类常量。

I suppose I could hard code the array in the static method, but then if I need to change it, I'd have to remember to change it in two places. I'd like to avoid this.

我想我可以在静态方法中硬编码数组,但是如果我需要修改它,我必须记住在两个地方修改它。我想避免这种情况。

3 个解决方案

#1


6  

You can create a private static function that will create the array on demand and return it:

您可以创建一个私有静态函数,根据需要创建数组并返回:

class YourClass {
    private static $values = NULL;
    private static function values() {
        if (self::$values === NULL) {
            self::$values = array(
                'value1',
                'value2',
                'value3',
            );
        }
        return self::$values;
    }
}

#2


0  

I put arrays in another file and then include the file wherever I need it.

我将数组放在另一个文件中,然后在需要的地方将文件包含进来。

#3


0  

I am having a really really hard time understanding your question. Here is essentially what I understood:

我真的很难理解你的问题。我的理解是:

I need to maintain a proper set, where no two elements are the same.

我需要维护一个合适的集合,其中没有两个元素是相同的。

PHP does not have a set type, not even in SPL! We can emulate the functionality of a set but any solution I can think of is not pleasant. Here is what I think is the cleanest:

PHP没有设置类型,甚至在SPL中也没有!我们可以模拟集合的功能,但我能想到的任何解决方案都不令人满意。以下是我认为最干净的:

<?php

class Set {

    private $elements = array();

    public function hasElement($ele) {
        return array_key_exists($ele, $elements);
    }

    public function addElement($ele) {
        $this->elements[$ele] = $ele;
    }

    public function removeElement($ele) {
        unset($this->elements[$ele]);
    }

    public function getElements() {
        return array_values($this->elements);
    }

    public function countElements() {
        return count($this->elements);
    }

}

Example usage:

使用示例:

<?php

$animals = new Set;
print_r($animals->getElments());
$animals->addElement('bear');
$animals->addElement('tiger');
print_r($animals->getElements());
$animals->addElement('chair');
$animals->removeElement('chair');
var_dump($animals->hasElement('chair'));
var_dump($animals->countElements());

#1


6  

You can create a private static function that will create the array on demand and return it:

您可以创建一个私有静态函数,根据需要创建数组并返回:

class YourClass {
    private static $values = NULL;
    private static function values() {
        if (self::$values === NULL) {
            self::$values = array(
                'value1',
                'value2',
                'value3',
            );
        }
        return self::$values;
    }
}

#2


0  

I put arrays in another file and then include the file wherever I need it.

我将数组放在另一个文件中,然后在需要的地方将文件包含进来。

#3


0  

I am having a really really hard time understanding your question. Here is essentially what I understood:

我真的很难理解你的问题。我的理解是:

I need to maintain a proper set, where no two elements are the same.

我需要维护一个合适的集合,其中没有两个元素是相同的。

PHP does not have a set type, not even in SPL! We can emulate the functionality of a set but any solution I can think of is not pleasant. Here is what I think is the cleanest:

PHP没有设置类型,甚至在SPL中也没有!我们可以模拟集合的功能,但我能想到的任何解决方案都不令人满意。以下是我认为最干净的:

<?php

class Set {

    private $elements = array();

    public function hasElement($ele) {
        return array_key_exists($ele, $elements);
    }

    public function addElement($ele) {
        $this->elements[$ele] = $ele;
    }

    public function removeElement($ele) {
        unset($this->elements[$ele]);
    }

    public function getElements() {
        return array_values($this->elements);
    }

    public function countElements() {
        return count($this->elements);
    }

}

Example usage:

使用示例:

<?php

$animals = new Set;
print_r($animals->getElments());
$animals->addElement('bear');
$animals->addElement('tiger');
print_r($animals->getElements());
$animals->addElement('chair');
$animals->removeElement('chair');
var_dump($animals->hasElement('chair'));
var_dump($animals->countElements());