0

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 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

2 Comments

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?
Well, it isn't exclusive to java. Just excluded from PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.