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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵