dsqtl335227 2016-02-16 05:10
浏览 29
已采纳

PHP OOP数组在方法中重复

Very new to OOP so please bear with me. I have a method that checks characteristics of a persons array and has a counter to keep track of things. The persons array starts off as serialized data, which I'm converting to an array with the unzerialize() function. When I do var_dump($this->people) in the constructor, I get the correct values. When I do the same in any of the other methods in the class, the array is duplicated a number of times and my counter produces incorrect values.

Also doing echo gettype($this->people) in the constructor results in array as expected, but in the other methods results in arrayarrayarrayarray....

What could be causing this duplication? And how do I solve it?

* EDIT *

Sorry for the confusion. I did not know where the problem was so it was difficult for me to explain properly.

The problem is not with my class definition, but how I am using the object. I have reworked the code to illustrate. I thought that once the object was instantiated, the values produced by it would be set, however it turns out that each time I call the get_results() method, $this->num is incremented and the value is persisted.

So my question is, how do I run the get_results() method once, and access the results[] array without changing the values? I was hoping I could do something like if($nums['results']['num'] == 1) but that results in a fatal error.

class DoStuff
{
    private $num;
    public $results;

    public function __construct($val)
    {
        $this->num = $val;
        $this->results = [];
    }

    private function compute_num()
    {
        $this->num++;
        $this->results['num'] = $this->num;
        return $this->results;
    }

    public function get_results()
    {
        $this->results = $this->compute_num();
        return $this->results;
    }   
}

$nums = new DoStuff(0);
$nums->get_results();

if($nums->get_results()['num'] == 1)
{
    printf('<p>(if) Num is: %d</p>', $nums->get_results()['num']);
}
else 
{
    printf('<p>(else) Num is: %d</p>', $nums->get_results()['num']);
}

// Prints (else) Num is: 3
  • 写回答

1条回答 默认 最新

  • donglieshe4692 2016-02-17 02:28
    关注

    Ok, so I was trying to access an object as an array. The solution is to use get_object_vars() after running the get_results() method.

    $nums = new DoStuff(0);
    $nums->get_results();
    $nums = get_object_vars($nums);
    
    if($nums['results']['num'] == 1)
    {
        printf('<p>(if) Num is: %d</p>', (int) $nums['results']['num']);
    }
    else 
    {
        printf('<p>(else) Num is: %d</p>', (int) $nums['results']['num']);
    }
    
    // Prints: (if) Num is: 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料