douniao7308 2018-08-17 14:51
浏览 16
已采纳

从服务器上其他位置的脚本实例化一个类OR:在脚本中访问const变量

I'm having trouble with instantiating a class from within a script. My code basically looks like this:

ConstAttributes.php located on server at e.g. /var/www/abc/def/

<?php
  namespace My\Path;

  class ConstAttributes {
    const ONE = "some";
    const TWO = "text";
    const THREE = "here";
  }
?>

index.php located elsewhere on server at e.g. /var/www/xyz/123/

<?php
  use My\Path\ConstAttributes;
  $aInst = new My\Path\ConstAttributes();
?>

I also tried:

use My\Path\ConstAttributes;
$aInst = new ConstAttributes();

but same result. I'm testing this live on an apache2 server. apache ist configured to point on the index page. When I'm refreshing the page, it's just blank - nothing on it. Everything that comes after I create the instance just isn't displayed at all; seems like the script hung himself up there. When I do something like this:

use My\Path\ConstAttributes;
//$aInst = new My\Path\ConstAttributes();
echo 'test';

I do get the echo message as expected.

The point in doing this is accessing the const variables in the index.php script. Before trying to instantiate the class, I tried ConstAttributes::ONE but that's dying there like it's doing when I'm instantiating the class.

I've googled quite alot now but couldn't solve the problem. Help would be much appreciated.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dsmgcse8876 2018-08-20 08:57
    关注

    If you try to use a constant within the php class, the php engine throws an exception " Notice: Use of undefined constant ONE - assumed 'ONE' in...". To work around this problem one can define and use the global constant. Please see the demo code here.

    //
    <?php
    /*
    * mypath\ConstAttributes.php
    */
    namespace MyPath2;
    //
    define("ONE1", "One1");
    const TWO2 = "Two2";
    define("SIX", "Six6");
    const SEVEN = "Seven7";
    define("EIGHT", "Eight8");
    const ONE = "some";
    const TWO22 = "text2";
    define("TWO", "text");
    const THREE = "here";
    //
    /**
    * Description of ConstAttributes
    *
    * @author B
    */
    class ConstAttributes {
    var $one = ONE;
    var $two = TWO;
    var $three = THREE;
    var $two2 = TWO2;
    var $four = "four4";
    var $five = "five5";
    var $seven = SEVEN ;
    var $eight = EIGHT ;
    
    function MyOne(){
        return ONE1;
    }
    function MyTwo(){
        return $this->two2;
    }
    function MyThree(){
        return $this->three;
    }
    function MyFour(){
        return $this->four;
    }
    function MySeven(){
        return $this->seven;
    }
    }
    //
    

    Once you have done that, then you can use your class as normal with index.php.

    //
    <!DOCTYPE html>
    <!--
    index.php
    -->
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Demo</title>
    </head>
    <body>
        <?php
        use MyPath2\ConstAttributes;
        include 'mypath\ConstAttributes.php';
        $aInst = new ConstAttributes();
        echo gettype($aInst)."<br>";
        echo $aInst->MyOne()."<br>";
        echo $aInst->MyTwo()."<br>";
        echo $aInst->MyFour()."<br>";
        echo $aInst->five."<br>";
        echo SIX."<br>";
        echo $aInst->MySeven()."<br>";
        echo $aInst->eight."<br>";
        echo "////////////////////////<br>";
        echo $aInst->one."<br>";
        echo TWO."<br>";
        echo $aInst->MyThree()."<br>";
        echo "///////////////////////////<br>";
        //echo TWO22."<br>";
        echo "///////////////////////////<br>";
        ?>
    </body>
    </html>
    //    
    

    The tested output is as follow:

    //////////////////output////////////
    // object
    // One1
    // Two2
    // four4
    // five5
    // Six6
    // Seven7
    // Eight8
    ////////////////////////
    // some
    // text
    // here
    ///////////////////////////
    ///////////////////////////
    //
    

    Enjoy!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题