【问题标题】:PHP: Calling a function within a return stringPHP:在返回字符串中调用函数
【发布时间】:2010-07-25 00:00:05
【问题描述】:

我试图在字符串返回语句中调用一个函数。两者都在同一个班级。但是,我显然没有正确调用它,因为它不起作用:)

private static function doSomething(){
   $d['dt'] = //unix time stamp here;

   return '<div class="date" title="Commented on '.date('H:i \o\n d M Y',$d['dt']).'">'.time_since($d['dt']).'</div>';
}

function time_since(){
   //return 'string';
}

感谢任何帮助!谢谢

【问题讨论】:

    标签: php string function call


    【解决方案1】:

    首先,您正在调用 time_since(),而您的函数是 time_stamp()。

    其次,在 PHP 中,您需要像 $this->time_stamp() 那样显式调用它 - 它不会像 C++ 那样将其解析为对象范围。

    第三,您不能从静态函数调用常规方法,因为该对象未实例化 - 也将 time_stamp() 设为静态。如果这样做,则需要像 self::time_stamp() 一样调用它。

    【讨论】:

    • 对不起,我的 time_stamp 是一个错字。所以,我现在就这样,无济于事。 (这些只是示例,而不是输入所有代码) private static function doSomething(){ $d['dt'] = //unix time stamp here; return '
      '.self::time_since($ d['dt']).'
      '; } public static function time_stamp($time){ //用接收到的字符串做一些事情 return 'new time string'; }
    【解决方案2】:

    当您列出的另一种方法是time_stamp() 时,代码是否意味着使用time_since()

    【讨论】:

    • 这应该是评论而不是答案。
    猜你喜欢
    • 1970-01-01
    • 2016-05-04
    • 2021-10-11
    • 2014-06-30
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2023-01-17
    • 2019-04-07
    相关资源
    最近更新 更多