Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating anonymous objects in php

Tags:

object

oop

php

People also ask

Can an object be anonymous?

Object expressions You can define them from scratch, inherit from existing classes, or implement interfaces. Instances of anonymous classes are also called anonymous objects because they are defined by an expression, not a name.

What are anonymous classes in PHP?

Anonymous classes ¶ Anonymous classes are useful when simple, one-off objects need to be created. All objects created by the same anonymous class declaration are instances of that very class. Note: Note that anonymous classes are assigned a name by the engine, as demonstrated in the following example.

Can anonymous classes have constructors PHP?

PHP Classes and Objects Anonymous Classes Anonymous classes were introduced into PHP 7 to enable for quick one-off objects to be easily created. They can take constructor arguments, extend other classes, implement interfaces, and use traits just like normal classes can.

What objects are anonymous objects?

An object which has no reference variable is called anonymous object in Java. Anonymous means nameless. If you want to create only one object in a class then the anonymous object is a good approach.


"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.


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.


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


Yes, it is possible! Using this simple 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.


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

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