【问题标题】:passing an instance of a class to another class in order to use its variables将一个类的实例传递给另一个类以使用其变量
【发布时间】:2015-03-22 17:45:59
【问题描述】:

我正在尝试将我的 Generator 类的实例传递给另一个类以使用一些变量。 Generator 类实例工作正常,但是当我将它传递给另一个(SelectStrategy)类时,它似乎根本没有传递变量。我不确定我做错了什么 - 我在被调用函数上使用了 var_dump 来检查它给了我什么,但它只是空白。

功能

class Generator 
{
    //properties for self
    private $s_charge;
    public $connection;
    public $task_priority;
    public $fog_mode = true;
    public $nodes = array();
    public $type;

    public function generateNodesSpecs() {
        $node = array();

        for ($i = 0; $i < 100; $i++) {

            $charge1 = mt_rand(30,100);
            $node['charge'] = $charge1;
            //array_push($node, $charge1);
            $hops = mt_rand(0,4);
            $node['hops'] = $hops;
            //array_push($node, $hops);
            $resource1 = mt_rand(0,100);
            if ($resource1 <= 50) {
                if ($resource1 <=10){
                    $node['connection'] = '4G';
                    //array_push($node, '4G');
                }
                else {
                    $node['connection'] = '3G';
                    //array_push($node, '3G');
                }
            }
            else if ($resource1 > 50 && $resource1 <= 60) {
                $node['connection'] = 'WiFi';
                //array_push($node, 'WiFi');
            }
            else {
            }
            $resource2 = mt_rand(0,100);
            if ($resource2 <=60) {
                $node['additional'] = 'CPU';
                //array_push($node, 'CPU');
            }
            else {
            $node['additional'] = 'none';
            }
            $this->nodes[] = $node;
            //array_push($nodes, $node);
            unset($node);

        }
    //compare which get the resources
    //var_dump($this->nodes[0]); 
    }
class SelectStrategy {
//take in generator class instance
    private $generator;
    private $priority;
    private $size;
    private $slaves = array();
    private $found_slave = null; //will hold item with max val;
    public function __construct($generator) {
        $this->generator = $generator;
    }
private function selectSlaves() {
        $max = -9999999; //will hold max val
        foreach($this->generator->nodes as $k=>$v)
        {
            if($v['charge']>$max)
            {
               $max = $v['charge'];
               $this->found_slave = $v;
            }
        }

        var_dump($this->found_slave); 

} 
}

还有类/函数调用

$generator = new Generator();
$generator->generateNodesSpecs();
$select_strategy = new SelectStrategy($generator);
$select_strategy->selectSlaves();

$this-&gt;generator-&gt;nodes 是一个二维数组

【问题讨论】:

  • 请同时发布Generator 代码。
  • 我看到的第一个问题是 $select_strategy = selectSlaves(); 它应该是 $select_strategy-&gt;selectSlaves(); 最重要的是,您的 Generator 实例中的属性需要公开或以其他方式访问,但我们需要您发布您的Generator 代码来确定那里的任何问题。
  • 如果 $nodes 是私有属性,那么您的 selectstrategy 将无法访问它。您将从生成器对象的“外部”进入,因此私有内容将是不可见/不可访问的。
  • 感谢您的指出,节点是公开的,我更正了错误,但仍然没有显示任何内容。我用生成器类更新了我的帖子。
  • 您的 Generator... 中有很多不带引号的数组键...例如:$node[connection] 应该是 $node['connection'] 或 $node[$this-&gt;connection] 如果它应该引用实例成员。但是你已经为charge 和其他一些人做了这个。

标签: php class parameter-passing instance-variables


【解决方案1】:
global $generator;

在 SelectSlave 的每个功能中都应该这样做

【讨论】:

  • 您的意思是将该行添加到 selectSlaves 函数中?还是编辑声明了生成器变量的每一行?我尝试将它添加到 selectSlaves() 函数,但它没有改变任何东西。
  • private function selectSlaves() { global $generator;//或者你的类在你的索引文件中声明为 $xx= new Generator;。并删除私有 $generator; $max = -9999999; //将保持最大值 foreach($this->generator->nodes as $k=>$v) { if($v['charge']>$max) { $max = $v['charge']; $this->found_slave = $v; } } var_dump($this->found_slave); }
猜你喜欢
  • 1970-01-01
  • 2011-03-15
  • 2023-01-25
  • 2021-07-25
  • 2019-05-02
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多