doutuoshou8915 2010-12-22 20:49
浏览 22
已采纳

too long

I have an index.php file that has 3 includes

<?php
require_once('mod.php');
$mod = new Mod();

require_once('start.php');
require_once('tools.php');

....some code....
?>

I need to be able to reference the $mod object inside the start.php and tools.php. How do I pass that object to be referenced by those 2 other require files?

Basically the mod.php is a class that has an array list generated in its __construct(). I want to use that array list data inside the startup.php and tools.php file but not sure how to pass in the existing one without calling "new" inside both of those files separately which doesn't do what I need since it resets everything.

Thanks!

  • 写回答

4条回答 默认 最新

  • dongshi9526 2010-12-22 20:54
    关注

    Doing require (or require_once, include, or include_once), simply includes and evaluates the code. The variable scope is inherited from the point at which the code is imported.

    As an example, using your code:

    <?php // index.php
    
    require_once('mod.php');
    $mod = new Mod();
    
    require_once('start.php');
    

    And the include:

    <?php // start.php
    
    $mod->arrayList(); // $mod is the object created in index.php
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示