tags:

views:

99

answers:

2

Hi folks,

Hopefully a quick and easy clarification only...With this code found in an action in a controller:

...         
$SaveAccount = new SaveAccount();
$SaveAccount->saveAccount($username, $password, $email);
...

Does the second line mean "run the method "saveAccount()" on the new object? Is that what the -> means? Thanks!

+8  A: 

The -> is used with objects. In below line:

$SaveAccount->saveAccount($username, $password, $email);

The saveAccountmethod is run of the object $SaveAccount

I would suggest you to have a look at:

Object Oriented Programming with PHP

Sarfraz
So I believe I was thinking correctly...Thanks!
Joel
@Joel: You are welcome
Sarfraz
+4  A: 

Does the second line mean "run the method "saveAccount()" on the new object?

Yes.

Is that what the -> means?

No, it means "fetch the method or property" named saveAccount. Together () it gains the meaning "run the method "saveAccount()". Note: technically, you cannot fetch a method without executing it, so $obj->methodname has no meaning without (), but this explanation may help you conceptually.

Artefacto
Cool-this is also helpful. Thank you!
Joel
Your "Note" is not exactly true. Have a look at this: http://ideone.com/ddUlj It is valid code that echoes "Bar" in PHP 5.3 (does not run on that site though because they use 5.2). You can "fetch" functions without executing them. In other languages that is common practice, for example in JavaScript. In PHP it is not used that often but it's absolutely possible and sometimes a nice feature to have.
Techpriester
@Techpriester That's not fetching a function, that's fetching a property that happens to be a lambda function. There are marked internal differences between the two and it's still true that you can't do `$a->foo = function () { }; $a->foo()`; you must use a temporary variable.
Artefacto
@Artefacto: That depends on the definition of "fetching". By "fetching" I'd understand something that returns the "fetched" thing. In this case, the function and not its result. Calling a regular method should be named, well ... "calling".
Techpriester
@Tech Even if we disagree on what "fetching" means, in `$a->foo = function () { };` `foo` is **not a method**, it's a property. The note says "you cannot fetch a method without executing it".
Artefacto
@Tech If PHP really had proper first class functions that were used to implement methods, you could possibly do `$obj->methodname` which would return a function and `$obj->methodname()`, which would call the function. I'm saying the first is not possible, and for that reason, although you can reason that in `$obj->methodname()` you're fetching a method (a function) and then calling it -- thus satisfying my description of `->` as "fetching a method or property" --, that's not really what happens (you're doing just a `ZEND_INIT_METHOD_CALL`).
Artefacto