Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将此对象添加到此静态数组的奇怪行为_Php_Oop_Static Array - Fatal编程技术网

Php 将此对象添加到此静态数组的奇怪行为

Php 将此对象添加到此静态数组的奇怪行为,php,oop,static-array,Php,Oop,Static Array,现在我用这段代码进行测试: <?php class Alert { private $type; private $message; public static $_alerts = array(); public function add($type, $message) { $this->type = $type; $this->message = $m

现在我用这段代码进行测试:

<?php
    class Alert {
        private $type;
        private $message;
        public static $_alerts = array();

        public function add($type, $message) {
            $this->type = $type;
            $this->message = $message;
            self::$_alerts[] = $this;
        }
    }

    $alert = new Alert();

    $alert->add("warning", "test 1");
    $alert->add("error", "test 2");

    echo "<pre>";
    print_r(Alert::$_alerts);
    echo "</pre>";
为什么我添加的对象被更改


测试区域:

您看到的问题是因为您的代码正在两次更改同一对象。 第一次调用时,它将设置数据“警告”和“测试1”,第二次将覆盖这些值

您可以通过创建对象的新实例并添加数据来解决此问题:

Array
(
    [0] => Alert Object
    (
        [type:Alert:private] => warning
        [message:Alert:private] => test 1
    )

    [1] => Alert Object
    (
        [type:Alert:private] => error
        [message:Alert:private] => test 2
    )

)
这将产生以下结果:

    public function add($type, $message) 
    {
        $this->type = $type;
        $this->message = $message;
        self::$_alerts[] = clone $this;
    }

这是因为您的对象(即内部上下文中的$this)将不按值显示。要按值复制,您需要执行以下操作:

array(2) { [0]=> object(Alert)#1 (2) { ["type":"Alert":private]=> string(5) "error" ["message":"Alert":private]=> string(6) "test 2" } [1]=> object(Alert)#1 (2) { ["type":"Alert":private]=> string(5) "error" ["message":"Alert":private]=> string(6) "test 2" } } 作为替代方案,您需要实例化对象(例如,像
new self
-但是
clone
这样的构造在这里似乎更灵活)您希望复制的次数

顺便说一下,有一个简单的方法来了解发生了什么。使用代替-然后您将看到对象实际上是相同的。您的代码示例(即尚未修复复制的代码):

阵列(2){ [0]=> 对象(警报)#1(2){ [“类型”:“警报”:专用]=> 字符串(5)“错误” [“消息”:“警报”:专用]=> 字符串(6)“测试2” } [1]=> 对象(警报)#1(2){ [“类型”:“警报”:专用]=> 字符串(5)“错误” [“消息”:“警报”:专用]=> 字符串(6)“测试2” } }
-如您所见,对象在那里是相同的。

您将两个引用保存到$\u警报数组中的同一对象。您需要为每个警报创建一个新对象,或执行以下操作:


    public function add($type, $message) 
    {
        $this->type = $type;
        $this->message = $message;
        self::$_alerts[] = clone $this;
    }
array(2) { [0]=> object(Alert)#1 (2) { ["type":"Alert":private]=> string(5) "error" ["message":"Alert":private]=> string(6) "test 2" } [1]=> object(Alert)#1 (2) { ["type":"Alert":private]=> string(5) "error" ["message":"Alert":private]=> string(6) "test 2" } }
<?php
    class Alert {
        private $type;
        private $message;
        public static $_alerts = array();

        private function __construct($type,$message){
                $this->type=$type;
                $this->message = $message;
        }

        public function getMessage(){
                return $this->message;
        }

        public function getType(){
                return $this->type;
        }

        public static function add($type, $message) {
            self::$_alerts[] = new self($type,$message);
        }
    }


    Alert::add("warning", "test 1");
    Alert::add("error", "test 2");

    echo "<pre>";
    print_r(Alert::$_alerts);
    echo "</pre>";