dtj4307 2010-04-10 11:04
浏览 48
已采纳

PHP - 连接对象和转换为字符串 - 坏主意?

Is it bad practice to concatenate objects when used in this context:

$this->template->head .= new View('custom_javascript')

This is the way i normally add extra css/js stuff to specific pages. I use an MVC structure where my basic html template has a $head variable which I set in my main Website_controller. I have used this approach for a while as it means I can just add bits and pieces of css/js stuff from whichever page/controller needs it. But having come across a problem in PHP 5.1.6 where the above code results in "Object ID #24", the result of toString() not being called i think, I am rethinking whether i should just fix this to work in PHP 5.1.6 or if i should rethink this approach in general.

Any pointers appreciated!

  • 写回答

4条回答 默认 最新

  • dsgw8802 2010-04-10 11:10
    关注

    You can always use View as a factory object, passing it as string to template with a render() function ( or whatever you'd like to call it ), so the code you have there would look something like;

    $this->template->head .= View::factory('custom_javascript')->render()
    

    being rendered the moment before it's passed to template.

    Kohana 3 for example uses this pattern to work with views / templates, you can read more about it at unofficial wiki

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?