ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php – 使用heredoc语法访问字符串中的静态变量的正确方法?

2019-10-07 06:30:17  阅读:189  来源: 互联网

标签:heredoc php string


假设我的班级中有一个名为$_staticVar的静态变量,我试图像这样访问.该变量有一个成员aString,其字符串值为“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;

输出:

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/>

heredoc PHPdocs对此没有任何说明.

我试过这个:

    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;

它不起作用.
输出:

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/>

这是我的PHP设置:

display_startup_errors = on
display_errors = On
error_reporting = E_ALL | E_STRICT

解决方法:

我很确定你必须使用本地或导入的变量进行字符串插值.最简单的解决方案?为什么,当然是当地的:

    $_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;

至于你的例子不起作用的原因:

    echo <<<eos

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

eos;

插入未定义的变量$something和$_staticVar,这会产生一个空字符串和一个通知.

    echo <<<eos

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

eos;

插值绝对不存在但永远不会存在的东西,这一切都让人感到困惑,但你知道它不起作用.

标签:heredoc,php,string
来源: https://codeday.me/bug/20191007/1865333.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有