NYCPHP Meetup

NYPHP.org

[nycphp-talk] Functions instantiating from Classes

Leam Hall leamhall at gmail.com
Fri Oct 11 07:33:05 EDT 2013


On 04/02/2013 10:18 PM, Gary A Mort wrote:
> On 2/25/2013 8:46 AM, Chris Snyder wrote:
>> On Sun, Feb 24, 2013 at 7:24 AM, Leam Hall <leamhall at gmail.com
>> <mailto: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
>>
>>
>> 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.
>
>
> I'm not so sure, he is "storing" the data for the purpose.  The purpose
> is to create a "group" of "people" based on certain parameters - and
> then display them to the end user.
>
> So each "generate" function outputs it's object data to the screen -
> thus "Storing" the data in the output.
>
> On the downside, one gotcha with this method of generation is that the
> web page will "generate" a new set of data every time it is loaded - so
> if the end user wants to print the data on the screen depending on the
> browser, it may completely resubmit the request so what is on the screen
> would not be what is printed out.
>
>
> I think you may want to also throw in some migration to using models and
> views as well - so for example
> generate_person: today it creates the stats and displays it
>
> Model/View
> generate_person: creates a person and returns the object
> print_person: displays the person data
>
> generate_squad: creates a squad object which contains an array of person
> objects
> print_squad: displays the squad data
>
>
> Also, as I'm assuming that this is being done as an exercise in learning
> and fun, you should not use global functions like generate_person,
> generate_squad, etc.
>
> You should either be namespacing your functions so as to get them out of
> the global namespace, ie:
> traveller/generate/person
> traveller/generate/squad
>
> Or you should look into the Factory pattern
> http://en.wikipedia.org/wiki/Factory_method_pattern for this sort of thing.
>
> And of course, most important, have fun, since that's what PHP
> programming is all about.
>
> :-)

Fun is the name of the game! It is also a great motivator.  :)

Now that I'm back to PHP, am looking at the Factory design pattern. 
Fortunately I have a copy of William Sanders' "Learning PHP Design 
Patterns". Never let it be said that I was short a book!

MVC is a later topic, though important. I need to be able to create web 
pages and to throw stuff into a database. So the implementations will be 
different but based off the same classes.

I am going back to the absolute basics and trying to make sure I 
understand each part. There is an abstract class "Being" that sets up 
the required properties but does not do anything. There is a helper 
class for dice rolls. There is a test_Dice.php file to test the dice rolls.

Two questions from this morning. First, is there a better way to test a 
(helper) class than what I did with test_Dice.php? I don't know PHP 
testing hardly at all, but want to add that skill.

Also, Dice.php should actually go into another project. If you're 
working on a project on GitHub that needs a file from another project, 
what's the best way to integrate it so that any changes get implemented?

Todays work:

https://github.com/makhidkarun/Traveller_Mercenary_Generator/blob/master/Dice.php

https://github.com/makhidkarun/Traveller_Mercenary_Generator/blob/master/test_Dice.php

Please keep in mind that I'm an OOP n00b, so there are probably lots of 
ways to improve. However, improvement is the goal. I prefer a suggestion 
or critical comment to a solution.

Thanks!

Leam

-- 
http://31challenge.net
http://31challenge.net/insight


More information about the talk mailing list