【问题标题】:php get the value of a constantphp获取常量的值
【发布时间】:2014-12-20 00:22:27
【问题描述】:

我想使用“test1”获取常量“test”的值。

我已经尝试过了,但只得到“test1”值而不是“test”值。

<?php

    class test
    {
        const test='fast';
        const test1=test;

        function testing()
        {

            echo test::test1;


        }
    }



<?php
include_once('class.test.php');

$d=new test;

echo $d->testing();

?>

有什么建议吗?

【问题讨论】:

    标签: php php-5.3 class-constants


    【解决方案1】:

    使用const test1 = self::test; 定义您的常量。

    【讨论】:

    • 另外,使用echo self::test1更简洁。
    • 不客气。如果您的问题得到解决,请随时关闭此问题。
    【解决方案2】:

    以下只是一个可行的解决方案,但您必须考虑很多事情,例如命名约定:

    <?php
        class test
        {
            const test='fast';
            const test1=test;
    
            function testing()
            {
    
                echo self::test1;
                echo self::test1;
    
            }
        }
    ?>
    <?php
    include_once('class.test.php');    
    $d=new test;    
    echo $d->testing();
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2020-12-20
      • 2011-09-03
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      • 1970-01-01
      相关资源
      最近更新 更多