douhuang75397 2017-02-03 17:09 采纳率: 0%
浏览 7
已采纳

如何使用extend从其他类访问变量?

Hello I would like to ask how can I get the $manufacturer and $id from the function postData() which included with other class. So I can pass it to my model. Thank you.

class postDataManager{

   public function postData(){
       $manufacturer = $_POST['manufacturer'];
       $id = $_POST['id'];  
   }
}

class manufactureController extends postDataManager{

  private $model;

    public function __construct(){
       $this->model = new manufacturerModel();
       $postDataManager= new postDataManager();
    }

   public function addManufacturer(){ //get the add function
      //here i need access for the variable $manufaturer from class postData function
       $this->model->addProcess($manufacturer);     
   }

   public function updateManufacturer(){ //get the update function
      //here i need access for the variable $manufaturer and $id from class postData function
       $this->model->updateProcess($id, $manufacturer);
   }
}
  • 写回答

3条回答 默认 最新

  • dpnfxk251524 2017-02-03 17:14
    关注

    Currently those two variables are lost once the postData() method is left, since they belong to the local scope of the method. You need to define properties for them.

    Take a look at this modified example:

    <?php
    class manufacturerModel {
    }
    
    class postDataManager {
      protected $id;
      protected $manufacturer;
    
      public function __construct($manufacturer, $id) {
        $this->manufacturer = $manufacturer;
        $this->id = $id;  
      }
    }
    
    class manufactureController extends postDataManager {
      private $model;
    
      public function __construct($manufacturer, $id) {
        parent::__construct($manufacturer, $id);
         $this->model = new manufacturerModel();
      }
    
      public function addManufacturer() { //get the add function
         $this->model->addProcess($this->manufacturer);     
      }
    
      public function updateManufacturer() { //get the update function
         $this->model->updateProcess($this->id, $this->manufacturer);
      }
    
      public function echoContent() {
        echo sprintf("manufacturer: %s
    id: %s
    ", $this->manufacturer, $this->id);
      }  
    }
    
    // some example values
    $_POST['manufacturer'] = "Manufactum Ltd.";
    $_POST['id'] = 17397394;
    
    $controller = new manufactureController($_POST['manufacturer'], $_POST['id']);
    $controller->echoContent();
    

    Now those values are stored in a persistent manner inside the object. Since your second class extends the first class those properties are also part of objects instantiate from that derived class, so you can access them likewise using the $this reference, unless they have been declared private in that class.

    The output of above demo code is:

    manufacturer: Manufactum Ltd.
    id: 17397394
    

    These are the basics of OOP (object oriented programming), this is explained in every tutorial.

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

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建