Creating anonymous objects in php

PhpOopObject

Php Problem Overview


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

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

alert(object.p1[1]);

Output:

an alert is raised with value "johnny"

Can this same technique be applied in PHP? Can we create anonymous objects in PHP?

Php Solutions


Solution 1 - Php

"Anonymous" is not the correct terminology when talking about objects. It would be better to say "object of anonymous type", but this does not apply to PHP.

All objects in PHP have a class. The "default" class is stdClass, and you can create objects of it this way:

$obj = new stdClass;
$obj->aProperty = 'value';

You can also take advantage of casting an array to an object for a more convenient syntax:

$obj = (object)array('aProperty' => 'value');
print_r($obj);

However, be advised that casting an array to an object is likely to yield "interesting" results for those array keys that are not valid PHP variable names -- for example, here's an answer of mine that shows what happens when keys begin with digits.

Solution 2 - Php

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.

Solution 3 - Php

Up until recently this is how I created objects on the fly.

$someObj = json_decode("{}");

Then:

$someObj->someProperty = someValue;

But now I go with:

$someObj = (object)[];

Then like before:

$someObj->someProperty = someValue;

Of course if you already know the properties and values you can set them inside as has been mentioned:

$someObj = (object)['prop1' => 'value1','prop2' => 'value2'];

NB: I don't know which versions of PHP this works on so you would need to be mindful of that. But I think the first approach (which is also short if there are no properties to set at construction) should work for all versions that have json_encode/json_decode

Solution 4 - Php

Yes, it is possible! Using this simple https://gist.github.com/3700483">PHP Anonymous Object class. How it works:

// define by passing in constructor
$anonim_obj = new AnObj(array(
    "foo" => function() { echo "foo"; }, 
    "bar" => function($bar) { echo $bar; } 
));

$anonim_obj->foo(); // prints "foo"
$anonim_obj->bar("hello, world"); // prints "hello, world"

// define at runtime
$anonim_obj->zoo = function() { echo "zoo"; };
$anonim_obj->zoo(); // prints "zoo"

// mimic self 
$anonim_obj->prop = "abc";
$anonim_obj->propMethod = function() use($anonim_obj) {
    echo $anonim_obj->prop; 
};
$anonim_obj->propMethod(); // prints "abc"

Of course this object is an instance of AnObj class, so it is not really anonymous, but it makes possible to define methods on the fly, like JavaScript do.

Solution 5 - Php

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

$obj = (object)  ['myProp' => 'myVal'];

Solution 6 - Php

If you wish to mimic JavaScript, you can create a class Object, and thus get the same behaviour. Of course this isn't quite anonymous anymore, but it will work.

<?php 
class Object { 
    function __construct( ) { 
        $n = func_num_args( ) ; 
        for ( $i = 0 ; $i < $n ; $i += 2 ) { 
            $this->{func_get_arg($i)} = func_get_arg($i + 1) ; 
        } 
    } 
} 

$o = new Object( 
    'aProperty', 'value', 
    'anotherProperty', array('element 1', 'element 2')) ; 
echo $o->anotherProperty[1];
?>

That will output element 2. This was stolen from a comment on PHP: Classes and Objects.

Solution 7 - Php

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();

Solution 8 - Php

For one who wants a recursive object:

$o = (object) array(
	'foo' => (object) array(
		'sub' => '...'
	)
);

echo $o->foo->sub;

Solution 9 - Php

From the PHP documentation, few more examples:

<?php

$obj1 = new \stdClass; // Instantiate stdClass object
$obj2 = new class{}; // Instantiate anonymous class
$obj3 = (object)[]; // Cast empty array to object

var_dump($obj1); // object(stdClass)#1 (0) {}
var_dump($obj2); // object(class@anonymous)#2 (0) {}
var_dump($obj3); // object(stdClass)#3 (0) {}

?>

$obj1 and $obj3 are the same type, but $obj1 !== $obj3. Also, all three will json_encode() to a simple JS object {}:

<?php

echo json_encode([
    new \stdClass,
    new class{},
    (object)[],
]);

?>

Outputs:

[{},{},{}]

https://www.php.net/manual/en/language.types.object.php

Solution 10 - Php

Anoynmus object wiki


$object=new class (){


};

Solution 11 - Php

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

Solution 12 - Php

> Can this same technique be applied in case of PHP?

No - because javascript uses prototypes/direct declaration of objects - in PHP (and many other OO languages) an object can only be created from a class.

So the question becomes - can you create an anonymous class.

Again the answer is no - how would you instantiate the class without being able to reference it?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSujit AgarwalView Question on Stackoverflow
Solution 1 - PhpJonView Answer on Stackoverflow
Solution 2 - PhpRizier123View Answer on Stackoverflow
Solution 3 - PhpZuksView Answer on Stackoverflow
Solution 4 - PhpMihailoffView Answer on Stackoverflow
Solution 5 - PhpT.ToduaView Answer on Stackoverflow
Solution 6 - PhpkbaView Answer on Stackoverflow
Solution 7 - Phpmiken32View Answer on Stackoverflow
Solution 8 - PhpJonatas WalkerView Answer on Stackoverflow
Solution 9 - PhpGlorious KaleView Answer on Stackoverflow
Solution 10 - Phpdılo sürücüView Answer on Stackoverflow
Solution 11 - PhpfredtmaView Answer on Stackoverflow
Solution 12 - PhpsymcbeanView Answer on Stackoverflow