NYCPHP Meetup

NYPHP.org

[nycphp-talk] Functions instantiating from Classes

Chris Snyder chsnyder at gmail.com
Mon Feb 25 08:46:22 EST 2013


On Sun, Feb 24, 2013 at 7:24 AM, Leam Hall <leamhall at gmail.com> wrote:

> OOP Newbie question as I convert a crufty program to OOP
>
> Class Person sets up various properties of a person. Classes B, C, and D
> extend A in various ways for different roles people do.
>
> What I ran into last night was trying to use a function outside of the
> classes to instantiate objects. For example:
>
> Function Z instantiates 3 objects of class B, one each object of class C.
>
> Function Y instantiates one instance of class D and then runs Function Z 3
> times, for a total of 16 objects.
>
> Function X creates one object of Class D and then calls Function 7 3 times.
>
> What I don't know, and couldn't clearly explain on ##php last night, is
> how to have Functions X, Y, and Z be able to create objects from classes B,
> C, and D.
>
> Any help, or good pointers to tutorials? Should I even be using functions?
> The goal is to create groups of objects based off group membership
> guidelines.
>
> If you're well and truly bored, the full code is at:
>
> https://github.com/LeamHall/**LHTraveller_Mercenary_**Generator<https://github.com/LeamHall/LHTraveller_Mercenary_Generator>
>
>
Not so bored, so only took a quick look.

It seems like you're calling functions to generate objects but not
returning those objects or storing them in a global array anywhere?

You are absolutely right to be using objects for this stuff.

There's not really any difference between using a function to instantiate
objects and using a function to instantiate any other variable. You have to
return (or otherwise store) the instantiated variables if you want them to
be available outside the function scope.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20130225/9b5473d2/attachment.html>


More information about the talk mailing list