dongsheng1698 2014-05-10 04:37
浏览 45
已采纳

为什么这个函数不能修改数组的全局版本?

I have defined a variable in what I think is global scope. I want to modify the same variable inside a function that lives in a class, then use it later to export as json data.

The function is called by the xataface api, so I'm not sure I can mess with the function signature to do something like passing by reference. I've thought I might access the instance of this action class in the javascript embedded php, but I don't know how to ask the api for it, nor am I confident of its lifetime. It seems like a global variable may be the way to go. In any case, I want to know:

  1. Why is not the global instance of $dataset1 the one being modified inside the function?
  2. Why doesn't the call to array_push put anything on either array?
<?php
//non-dynamic data delcared in global scope. This is picked up later
//in a php block embedded into javascript
$dataset1 = array(array("label"=>"c120","data"=>"1"),
                  array("label"=>"c150","data"=>"10"),
                  array("label"=>"camp","data"=>"7"));
class actions_time_in_type
{
  function handle(&$params)
  {
    $this->app =& Dataface_Application::getInstance(); 
    //The Query
    $result = mysql_query("SELECT typeDes, total
                           FROM myTable", $this->app->db());
    //reserch leads me to believe that this *should* make all subsequent
    //references to $dataset1 use the global instance
    global $dataset1;
    //experimenting with appending more non-dynamic data
    //for some reason, this syntax does not seem to touch $dataset1 
    array_push($dataset1, array("label"=>"dv20","data"=>"1"));
    //This syntax is working, but $dataset1 is not the same as the global 
    //$dataset1. Prepending "global" here seems to crash the script
    $dataset1[] = array("label"=>"pa18","data"=>"5");
    while($row = mysql_fetch_assoc($result))
    {
       //append data to the array, again, this is not hitting
       //the global instance of $dataset1
       $dataset1[] = array("label"=>$row['typedes'],"data"=>$row['total']);
    }
    mysql_free_result($result); //Frees the result after finished using it           
    //diagnostic dump to see what we've got
    //This shows that we've constructed the dynamic data set, but it
    //seems to be scoped only to this function and does not make it into
    //javascript.
    var_dump($dataset1);
  }
}
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" ></script>
<script type="text/javascript">
$(function () {
   //This is getting only what was done original init of $dataset1, nothing that
   //happened in the function made a difference
   var dataset1 = <?php echo json_encode($dataset1); ?>;
});
</script>
  • 写回答

1条回答 默认 最新

  • douxie2029 2014-05-26 03:01
    关注

    It appears this is indeed a problem with execution order. Since the manipulation of the data happens within a class definition which in turn is called by the api, there appears to be no assurance that the global definition of the data will be in scope when it is time to manipulate the data, or that it will have been manipulated when the data is re-used further down.

    For the interested reader, I was able to get the api developer to demonstrate the proper way of integrating javascript with the api calling the in-class function:

    https://groups.google.com/forum/#!topic/xataface/l6qBzxF1vrc

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64