douniao7308
douniao7308
2018-08-17 14:51

从服务器上其他位置的脚本实例化一个类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 dsmgcse8876 3年前

    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!

    点赞 评论 复制链接分享