【问题标题】:Using $this when not in object context model class不在对象上下文模型类中时使用 $this
【发布时间】:2016-08-12 15:32:58
【问题描述】:

我已经说明了我的定制 MV,我有一个简单的模型 + 控制器类,当被不同的函数调用时,我无法让模型中的 var 被识别

控制器类是

  class StaffController {

    protected $module = "staff";

    public function __construct()
    {
        //include the staff Model
        require_once(MODEL_PATH.$this->module.'.php');
    }
    public function index() {

        // we store all the posts in a variable
        $staff = Staff::all();

        header('Content-Type: application/json');
        echo json_encode($staff);

    }

$staff = Staff::all(); 调用模型类,它的$list 变量无法识别:

  class Staff {

    public $list = array();

    public function __construct() {
          $this->list = [];
    }


    public static function all() {
      //get all the staff 

      $temp = [];

      $db = Db::getInstance();

      $req = $db->query('SELECT * FROM data ORDER BY ParentID ASC');

      // we create a list of Post objects from the database results
      foreach($req->fetchAll() as $staff) { array_push($temp,$staff);

      self::reorderOrg($temp );

      return $final;
    }


    private static function reorderOrg($array, $parent = 0, $depth = 0){
          //reorganise org 

        for($i=0, $ni=count($array); $i < $ni; $i++){
            //check if parent ID same as ID

            if($array[$i]['parentID'] == $parent){

                array_push($this->list ,$array[$i]); //***** no being recognized

                self::reorderOrg($array, $array[$i]['id'], $depth+1);

            }
        }

        return true;
      }


  }

我收到以下错误在不在对象上下文中使用 $this 以及它与模型类中的 array_push 不喜欢 $this-&gt;list 有关。如何设置私有变量,使其可以在自己的类函数中使用

【问题讨论】:

  • 静态方法中没有“this”上下文。你必须打电话给self::$list 而不是$this-&gt;list

标签: php class protected


【解决方案1】:

static 关键字意味着该函数是在类本身上调用的,而不是在类的实例上。这意味着$this 没有引用任何内容。

如果它是一个需要在特定实例上调用的函数,以便您可以访问其成员,则需要将其设为非静态或传入该类的实例(可能是前者,因为这就是非静态方法的用途)。

就让类保留其实例列表而言:您​​在这里所做的是在您的类的 instance 上初始化 list,因此每个实例都有一个空列表.这可能不是您想要的。

【讨论】:

    【解决方案2】:

    你不在对象上下文中,因为你的函数是静态。您必须改用selfself 指的是当前类:

    array_push(self::list, $array[$i]);
    

    【讨论】:

      【解决方案3】:

      您不能在静态方法中使用$this,因为静态方法不是$this 所指的实例化对象的一部分。即使对象没有实例化,也可以调用静态方法。

      您必须使 reorderOrg 非静态才能工作。

      【讨论】:

        【解决方案4】:

        请阅读 PHP 中的静态方法和属性。

        你不能在静态方法中访问$this,因为你没有任何应该被称为$this的类的实例。

        这里有两种选择

        1. 将属性声明为静态并使用self 关键字访问它。例如

          // 声明

          公共静态 $list = array();

          // 访问

          self::$list[] = '某事';

        2. 创建类的对象并访问创建对象的属性。

          // 创建对象

          $staff = 新员工();

          // 访问

          $staff->list[] = '某事';

        记得阅读documentation

        【讨论】:

          【解决方案5】:

          相关帖子:Using this inside a static function fails

          这是否解决了您的问题?使用静态方法时;你必须使用 self:: 而不是 $this->

          http://php.net/manual/en/language.oop5.static.php

          array_push(self::list,...);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-02-09
            • 2015-02-12
            • 2023-03-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多