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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 libreoffice导出PPTX到PDF中的错误换行问题
  • ¥15 python实现网页视频下载
  • ¥15 如何通过深度图获得物体的完整、正确点云?
  • ¥15 找一本C#4.5参考大全电子书
  • ¥15 有没有操作系统适用白丁的经典的书籍,如何解决?(标签-学习)
  • ¥15 Catia V5 R20 64位 安装过程中选择orbix配置创建套接字失败
  • ¥100 C51单片机设计交通灯时出现的问题
  • ¥15 R语言爬虫的时候元素和园代码不一样怎么解决呀
  • ¥15 SQL删除添加数据后序号不连续问题。
  • ¥15 首次运行OmniEvent运行报错