views:

40

answers:

1

Please help me understand:

print gettype(new CustomerObject()) 

prints: "object" (so it is an object)

BUT

print gettype((new CustomerObject())->get_customer());

prints: unexpected T_OBJECT_OPERATOR

If I do it over two lines it works fine

$object = new Customer($order->customer_id);
print gettype($object);

prints: object

$customer = $object->get_customer();
print gettype($customer);

prints: array

It appears that the lines cannot be joined into a single call. Is this correct? and what is the logic behind that?

+1  A: 

You are a little confused

print gettype((new CustomerObject())->get_customer());

Tries to call the method get_customer() on what gettype returns. (A string isn't an object)

Basically, if you want to create an object, and then call a method on it, you have to do it on two separate lines.

This no worky:

$array = new Object->getArray();

This worky:

$object = new Object;
$array = $object->getArray();
Chacha102
Ok, just as I thought... I've dabbled with java and you can call the method as a part of the object declaration... This is obviously a feature that is exclusive to java?
php-b-grader
Well, it isn't exclusive to java. Just excluded from PHP.
Chacha102