Question

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 case of PHP? Can we create anonymous objects in PHP?

Was it helpful?

Solution

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


In php-7 it is 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 RFC (It is accepted): https://wiki.php.net/rfc/anonymous_classes

But I don't know how similar it is implemented to Javscript, so their may be a few differences between anonymous classes in javascript and php.

Edit:

As from the comments posted, here is the link to the manual now: http://php.net/manual/en/language.oop5.anonymous.php

OTHER TIPS

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

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.

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

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.

Convert array to object:

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

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

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?

For one who wants a recursive object:

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

echo $o->foo->sub;

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top