douao7937 2017-08-02 04:16
浏览 14
已采纳

为什么这个PHP对象会将项目附加到自身?

I'm new to OOP and seem to be stuck on something that i'm sure is very simple, but I don't understand what's happening.

The below example code:

class add_some {

    static $arr = array('a', 'b');

    static $new_arr = array();

    public static function iterate() {

        foreach (self::$arr as $v) {
            self::$new_arr[] = $v;
        }

    }

}

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

    // instantiate the object
    $add_some = new add_some;

    // launch the method that copies items from $arr to $new_arr
    $add_some::iterate();

    echo '<pre>';
    print_r($add_some::$new_arr);
    echo "</pre>";

    unset($add_some);

}

Goes through a loop and adds the items which are in $arr to $new_arr, it basically copies all items from $arr to $new_arr

The code instantiates a new object and executes the method iterate() which copies items from $arr to $new_arr 3 times.

Each iteration creates a new object $add_some = new add_some; so each iteration should just produce:

Array
(
    [0] => a
    [1] => b
Array
(
    [0] => a
    [1] => b
)
Array
(
    [0] => a
    [1] => b
)

But for some reason the object keeps growing with each iteration.... And I have no idea why.. I tried destroying the object (unset) and not sure if I should be doing something else.

Array
(
    [0] => a
    [1] => b
)
Array
(
    [0] => a
    [1] => b
    [2] => a
    [3] => b
)
Array
(
    [0] => a
    [1] => b
    [2] => a
    [3] => b
    [4] => a
    [5] => b
)
  • 写回答

1条回答 默认 最新

  • drlh197610 2017-08-02 04:18
    关注

    You are calling the static method by use of the :: (Paamayim Nekudotayim), you should be using -> and not using a static function.

    Static means, keep this around globally, it is not instantiated each time you call new, it is static.

    You can for example call a static method like so:

    class MyClass {
      static function MyFunc() { echo 'Stuff'; }
    }
    
    MyClass::MyFunc()
    

    There is no need to (nor should you) create an instance of MyClass to call the static MyFunc.

    Each time you call iterate it is appending the global static array $arr

    The correct way to do this would be

    class add_some {
      private $x = 1;
      private $arr = array('a', 'b');
      public  $new_arr = array();
    
      public function iterate() {
        foreach ($this->arr as $v) {
            $this->new_arr[] = $v;
        }
      }
    }
    
    for ($i=0; $i < 3; $i++) {
      $add_some = new add_some();
      $add_some->iterate();
      print_r($add_some->new_arr);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?