Creating anonymous objects in php

前端 未结 12 1882
故里飘歌
故里飘歌 2020-11-28 01:59

As we know, creating anonymous objects in JavaScript is easy, like the code below:

var object = { 
    p : \"value\", 
    p1 : [ \"john\", \"johnny\" ]
};

         


        
相关标签:
12条回答
  • 2020-11-28 02:34

    If you want to create object (like in javascript) with dynamic properties, without receiving a warning of undefined property, when you haven't set a value to property

    class stdClass {
    
    public function __construct(array $arguments = array()) {
        if (!empty($arguments)) {
            foreach ($arguments as $property => $argument) {
                if(is_numeric($property)):
                    $this->{$argument} = null;
                else:
                    $this->{$property} = $argument;
                endif;
            }
        }
    }
    
    public function __call($method, $arguments) {
        $arguments = array_merge(array("stdObject" => $this), $arguments); // Note: method argument 0 will always referred to the main class ($this).
        if (isset($this->{$method}) && is_callable($this->{$method})) {
            return call_user_func_array($this->{$method}, $arguments);
        } else {
            throw new Exception("Fatal error: Call to undefined method stdObject::{$method}()");
        }
    }
    
    public function __get($name){
        if(property_exists($this, $name)):
            return $this->{$name};
        else:
            return $this->{$name} = null;
        endif;
    }
    
    public function __set($name, $value) {
        $this->{$name} = $value;
    }
    
    }
    
    $obj1 = new stdClass(['property1','property2'=>'value']); //assign default property
    echo $obj1->property1;//null
    echo $obj1->property2;//value
    
    $obj2 = new stdClass();//without properties set
    echo $obj2->property1;//null
    
    0 讨论(0)
  • 2020-11-28 02:35

    For one who wants a recursive object:

    $o = (object) array(
        'foo' => (object) array(
            'sub' => '...'
        )
    );
    
    echo $o->foo->sub;
    
    0 讨论(0)
  • 2020-11-28 02:40

    Convert array to object (but this is not recursive to sub-childs):

    $obj = (object)  ['myProp' => 'myVal'];
    
    0 讨论(0)
  • 2020-11-28 02:40

    Anoynmus object wiki

    
    $object=new class (){
    
    
    };
    
    
    0 讨论(0)
  • 2020-11-28 02:41

    Support for anonymous classes has been available since PHP 7.0, and is the closest analogue to the JavaScript example provided in the question.

    <?php
    $object = new class {
        var $p = "value";
        var $p1 = ["john", "johnny"];
    };
    
    echo $object->p1[1];
    

    The visibility declaration on properties cannot be omitted (I just used var because it's shorter than public.)

    Like JavaScript, you can also define methods for the class:

    <?php
    $object = new class {
        var $p = "value";
        var $p1 = ["john", "johnny"];
        function foo() {return $this->p;}
    };
    
    echo $object->foo();
    
    0 讨论(0)
  • 2020-11-28 02:43

    It has been some years, but I think I need to keep the information up to date!

    Since PHP 7 it has been possible to create anonymous classes, so you're able to do things like this:

    <?php
    
        class Foo {}
        $child = new class extends Foo {};
    
        var_dump($child instanceof Foo); // true
    
    ?>
    

    You can read more about this in the manual

    But I don't know how similar it is implemented to JavaScript, so there may be a few differences between anonymous classes in JavaScript and PHP.

    0 讨论(0)
提交回复
热议问题