Proper way to access a static variable inside a string with a heredoc syntax?

折月煮酒 提交于 2019-11-30 19:09:03

问题


Lets say I have a static variable called $_staticVar in my class which I am trying to access like this. The variable has a member aString which has the string value of "my static variable"

    echo <<<eos

    <br/>This is the content of my static variable, 
    self::$_staticVar->$aString
    which is not getting accessed properly in heredoc syntax. <br/>

eos;

Output:

Notice: Undefined variable: _staticVar in /path/to/file.php on line some_line_number

<br/>This is the content of my static variable,
self::->my static variable,
which is not getting accessed properly in heredoc syntax.<br/>

The PHPdocs for heredoc doesn't say anything about this.


I have tried this:

    echo <<<eos

    <br/>This is the content of my static variable,<br/>
    {${self::$_staticVar->$aString}}<br/>
    which is not getting accessed properly in heredoc syntax. <br/>

eos;

and it does not work.
Output:

Notice: Undefined variable: _staticVar in /path/to/file.php on line some_line_number

<br/>This is the content of my static variable,
   
which is not getting accessed properly in heredoc syntax.<br/>


This is my PHP setting:

display_startup_errors = on
display_errors = On
error_reporting = E_ALL | E_STRICT


回答1:


I'm fairly certain you must use a local or imported variable for string interpolation. The easiest solution? Why, make it local of course:

    $_staticVar = self::$_staticVar; // or did you mean self::_staticVar? Not too clear on that.

    echo <<<eos

    <br/>Something {$_staticVar->something} more of something <br/>

eos;

As for the reasons your examples didn't work:

    echo <<<eos

    <br/>Something self::$_staticVar->{$something} more of something <br/>

eos;

Interpolates undefined variables $something and $_staticVar, which results in an empty string and a notice.

    echo <<<eos

    <br/>Something {${self::$$_staticVar->{$something}}} more of something <br/>

eos;

Interpolates the value of something that definitely doesn't exist and never will and it's all really confusing but you know it doesn't work.




回答2:


You can do show at this sample class to show how to access/call a static method or attribute from inside a string.

You must store the classname inside a variable, so you can access classelements over this variable and yes you can access static variables and static methods.

<?php

class test {
    private $static = 'test';
    // static Method
    static function author() {
        return "Frank Glück";
    }
    // static variable
    static $url = 'http://www.dozent.net';
    public function dothis() {
       $self = __CLASS__;
       echo <<<TEST
           {${$this->self}}::author()}} // don't works
           {${!${''}=static::author()}} // works
           {$self::author()} // works
TEST;
    }
}

$test = 'test'; // this is the trick, put the Classname into a variable

echo "{$test::author()} {$$test::$url}";
echo <<<HTML
<div>{$test::author()}</div>
<div>{$$test::$url}</div>
HTML;


来源:https://stackoverflow.com/questions/8773236/proper-way-to-access-a-static-variable-inside-a-string-with-a-heredoc-syntax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!