doubu0897 2015-11-18 09:45
浏览 72
已采纳

在PhP中将变量从一个类传递到另一个类

Hello I am trying to build my first restful web service and im using the instruction from lorna jane mitchell blog.

If the req comes through this Url : http://localhost:8888/lorna/index.php/tree/getpath?node_id=75 i call the function getpath passing node_id The function get path looks like this :

  class NestedSet
    {
       public function getPath($id) {

    $sql = "SELECT p." . $this->pk . ", p." . $this->name . " FROM ". $this->table . " n, " . $this->table . " p WHERE n.lft BETWEEN p.lft AND p.rgt AND n." . $this->pk ." = " . $id . " ORDER BY p.lft;";
       $result = $this->db->query($sql);
       if ($result->num_rows == 0) {
       return $this->error(1, true);
       }
       $path = array();
       $i = 0;
       while ($row = $result->fetch_assoc()) {
        $path[$i] = $row;
        $i++;
       }
      return $path;
     }

    } 

Now i want to pass this variable $path to the class JsonView that looks like this :

    class JsonView extends ApiView {
public function render($path) {
    header('Content-Type: application/json; charset=utf8');
    echo json_encode($path);
    return true;
   }
   }





   class ApiView {
     protected function addCount($data) {
     if(!empty($data)) {
        // do nothing, this is added earlier
     } else {
        $data['meta']['count'] = 0;
       }
        return $data;

     } 
     }

Any Idea on how can I pass the variable $path or any other variable through this JsonView Class. Thank you very much for your time :)

UPDATE This is the code for creating the nested class object

public function getAction($request) {
  $data = $request->parameters;

  if(isset($request->url_elements[2])) {

    switch ($request->url_elements[2]) {
      case 'getpath':

      $id = $data['node_id'];

      $nested = new NestedSet();
      $nested->getPath($id);

      $api = new JsonView(); 
      $api->render($path); 

      break;

      default:
      # code...
      break;
    }
  } else {
    $nested = new NestedSet(); 
    echo $nested->treeAsHtml();
  }
}
  • 写回答

1条回答 默认 最新

  • dongluo3962 2015-11-18 09:52
    关注

    Just create object of JsonView and then call the function render using that object.

    $api = new JsonView;
    $api->render($path);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题
  • ¥15 有没有代做有偿主要做数据可视化部分即可(2023全国高考更省一本线理科类)