NYCPHP Meetup

NYPHP.org

[nycphp-talk] Optimal Object, property, variable passing strategy between classes

Flavio daCosta nyphp at n0p.net
Thu Jul 14 15:26:32 EDT 2005


here is my $.02

Of course everything really depends on each individual scenario, but
here goes:

On 07/14/2005 12:08 PM, Cliff Hirsch wrote:
> --snip --
> If I use arguments in functions, I believe the values are copied, which
> would seem to be slow. As one function calls another function and so on,
> it seems like needless copies of a variable are made. If I use &
> reference, there's no copy, but now the variable is "exposed" to change.

Variables:  If you have many that you are passing, I would encapsulate
them in an object otherwise I would copy if I was concerned about
tampering and reference if variable data is large.

> If I pass an object by reference, there's no copy, but once again, the
> properties are "exposed" and I still have the copy propagation problem
> with multiple nested function calls.

If you have accessors methods, you _should_ be able to protect against
un-authorized changes, no?

> Would the best way be to pass an
> object in the argument of the constructor to capture values for all
> function within the new class?

I find passing an object in the constructor works well, especially for
substituting the _passed_ object with a mock object in order to test.

You could also utilize the Singleton Design pattern.

YMMV
Flavio



More information about the talk mailing list