1

WebアプリケーションにYiiを使用しています。これで私は定数クラスをモデルに保持し、拡張しました

CUserIdentityように..

class Constants extends CUserIdentity
{
 CONST ACCOUTN_ONE = 1;
 CONST ACCOUTN_TWO = 2;
 CONST ACCOUTN_THREE = 3;
}

ここでは、のような定数にアクセスでき、次のようConstants::ACCOUTN_ONEに正しい結果が返されます。1

しかし、私が定数の構築を開始すると、動的に意味します。

$type = 'ONE';
$con = "Constants::ACCOUTN_".$type;
echo $con;

Constants::ACCOUTN_ONEとして表示されます。

私はここで期待しています1

間違いがあれば訂正してください。

4

3 に答える 3

1
$type = 'ONE';
$con = "Constants::ACCOUTN_".$type;
echo Constant($con);
于 2013-01-16T13:24:23.203 に答える
0
$type = 'ONE'; // You created string

$con = "Constants::ACCOUTN_".$type; // Created other string

echo $con; // Printed it

文字列を評価せずに印刷しただけです。
はい、当然のことながら、Constants::ACCOUTN_ONEとして表示されます。

eval()コードを(bad)で評価するか、次のスキームを使用する必要があります。

echo Constant($con);

于 2013-01-16T13:21:57.357 に答える
-1

私はしばらく前にそれのためのクラスでこれを行います:

/**
 * Lots of pixie dust and other magic stuff.
 *
 * Set a global: Globals::key($vlaue); @return void
 * Get a global: Globals::key(); @return mixed|null
 * Isset of a global: Globals::isset($key); @return bool
 *
 * Debug to print out all the global that are set so far: Globals::debug(); @return array
 *
*/
class Globals
{

    private static $_propertyArray = array();

    /**
     * Pixie dust
     *
     * @param $method
     * @param $args
     * @return mixed|bool|null
     * @throws MaxImmoException
     */
    public static function __callStatic($method, $args)
    {
        if ($method == 'isset') {
            return isset(self::$_propertyArray[$args[0]]);
        } elseif ($method == 'debug') {
            return self::$_propertyArray;
        }

        if (empty($args)) {
            //getter
            if (isset(self::$_propertyArray[$method])) {
                return self::$_propertyArray[$method];
            } else {
                return null; //dont wonna trow errors when faking isset()
            }
        } elseif (count($args) == 1) {
            //setter
            self::$_propertyArray[$method] = $args[0];
        } else {
            throw new Exception("Too many arguments for property ({$method}).", 0);
        }

    }
}
于 2013-01-16T13:51:57.387 に答える