关于PHP连接对象

Could somebody explain to me why the following is wrong :

$sql='SELECT * FROM images';
$hostname ='localhost';
$username= 'root';
$password= '';
$db= 'myDB';

$numRows=new mysqli($hostname, $username, $password, $db)->query($sql)-> num_rows;

The error throws is : syntax error, unexpected T_OBJECT_OPERATOR .....

and is it possible to correct it in one statement like above ? Thank you.

Chain after your instantiate mysqli.

$m = new mysqli($hostname, $username, $password, $db);
$numrows = $m->query($sql)->num_rows;

Place brackets around new mysqli(....).

ie. Place the following:

$numRows=(new mysqli($hostname, $username, $password, $db))->query($sql)-> num_rows;

Instead of:

$numRows=new mysqli($hostname, $username, $password, $db)->query($sql)-> num_rows;

But as @MarcB said, This is a very hideous thing to do. Later, debugging would be difficult.