Calling a function on a newly created object;

Is it possible to do something like this:

$object = new object()->getString();

echo $object;

instead of

$object = new object();
$string = $object->getString();

echo $string;

–Class–
class object {

var $str=“”;

function object() {
$this->str= rand(1,100);
}

function getString() {
return $this->str;
}

}

Awesome, thank you guys then :slight_smile:

Is it possible to do something like this:

No that is illegal syntax, AFAIK, in PHP. Although I have often thought that would be handy. The best alternative is to use a factory method, something like:

function createObject($class_name, $ctor_params = null)
{
  return new $class_name($ctor_params);
}

createObject('Some_Class')->doSomething();

Cheers,
Alex

Alex is correct, it is invalid.