Say I have a simple class and I create it and call a function on it like this:
class tst
{
private $s = "";
public function __construct( $s )
{
$this->s = $s;
}
public function show()
{
return $this->s;
}
}
$t = new tst( "hello world" );
echo "showing " . $t->show() . "\n";
Is there any syntax or workaround that will allow me to instantiate an instance of tst and call the show() function without assigning the object to a variable? I want to do something like:
echo new tst( "again" )->show();
I don't want to declare my functions as static as I want to use them in both of the above examples.
tst::create(x)->show()over just usingx?!? Orf(x)if there's actually some work (validation, normalization, conversion) ones in the constructor?(function (x) { return x; })(x)in JS, if you will. And even if the constructor did something useful with that value, there's no need to wrap the result in an extra object.