简体   繁体   English

访问另一个类中的一个类的数组,而无需在 php 中传递参数

[英]Access an array of a class in another class without passing arguments in php

$mile= new mile();

  $get_sales_totals1=mysql_query("SELECT title,deadline FROM milestones where id=$id ");


  while($milestone = mysql_fetch_array($get_sales_totals1)){

    $mile->addne($milestone["deadline"],$milestone["title"]);
 }

I am trying to create an array of objects from database query.Above code is the main.php page and below is the page with classes.I created an array with deadline and title in each item of the array.我试图从数据库查询创建一个对象数组。上面的代码是 main.php 页面,下面是带有类的页面。我在数组的每个项目中创建了一个带有截止日期和标题的数组。

class mile {

    public $milearray;

public function __construct(){
    $this->milearray=array();

    }

    public function addne($deadline,$title){
     $ne->deadline=$deadline;
     $ne->title=$title;
     array_push($this->milearray,$ne);
    }

    public function extra(){

    //how to get milearray $this->milearray here

    }

}

Is it possible to get milearray in the function 'extra' in the same class without passing arguments.是否可以在不传递参数的情况下在同一类中的“额外”函数中获得 milearray。

OR或者

class compare is incomplete.类比较不完整。 I need to call milearray from class 'mile' inside class 'compare' without passing any arguments我需要在类 'compare' 中从类 'mile' 调用 milearray 而不传递任何参数

    class compare{
   public daycomparearray;
    public function __construct(){
     $this->daycomparearray=array();

    }
    public function comparemile(){
     if($this->daycomparearray== milearrray->deadline)
    // how to get mile array here 
    }
    }

please help me....请帮我....

You have to pass arguments, otherwise the compare class won't know which objects to compare.您必须传递参数,否则compare类将不知道要比较哪些对象。

class compare{
    public function __construct(){

    }
    public function comparemile($mile1, $mile2){
        $array1 = $mile1->milearray;
        $array2 = $mile2->milearray;
        // code that compares $array1 and $array2
    }
}

If you can't pass arguments to the comparemile function, maybe it can be done with the constructor.如果您不能将参数传递给comparemile函数,也许可以通过构造函数来完成。

class compare{
    private $array1;
    private $array2;

    public function __construct($mile1, $mile2){
        $this->array1 = $mile1->milearray;
        $this->array2 = $mile2->milearray;

    }
    public function comparemile(){
        // code that compares $this->$array1 and $this->$array2
    }
}

You tried self ?你试过self吗? Like:喜欢:

public function extra(){
   $array = self::$milearray;
}

or making a session for global vars?或者为global变量创建一个会话?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM