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 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题