dongpo9071 2011-08-31 20:01
浏览 81

可以从PHP类中的include()'d文件访问$ this吗?

I'm working on a WordPress Widget and the examples all have huge HTML/PHP chunks intermixed and it is impossible to read, so in the interest of trying to clean stuff up I'd like to move all of the HTML rendering to a separate PHP file that will be include()'d.

The trick to this is, the file I include doesn't appear to have access to $this and I'm unsure how to fix that.

widget.php

class Preorder extends WP_Widget {
    ...
    function form() {
        include('form.php');
    }
}

form.php

<p>
   <?php echo $this->get_field_id('title'); ?>
</p>

Which results in [31-Aug-2011 19:59:19] PHP Fatal error: Call to a member function get_field_id() on a non-object in ... so clearly $this doesn't come along for free. I've tried aliasing $this to another variable & even just for fun using the global keyword without success.

Hopefully I missed something easy.

  • 写回答

1条回答 默认 最新

  • douhuang5331 2011-08-31 20:14
    关注

    Forgot that I was iterating over the directory & include-ing every .php file, renamed form.php to form.tmpl and appears to work.

    Durr.

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行