design pattern for a series of tasks

what is a design pattern


$user1= new Instructions($user_name);

$user1->select_rows_of_table_in_mysql($subject, $body, $email_addresses);
$user1->log_in_to_phpmailer($user_name);
$user1->send_emails_using_phpmailer($user_name);
$user1->insert_phpmailer_errors;

and then repeat for $user2, $user3

And then if I wanted to see the data from the table
$user1->see_phpmailer_errors($user_name) for each user


I think that I am copy pasting too many instances for each task
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
It depends on what your tasks are doing.

For instance you could have a loop going through a list of users (array or resultset from a database) - for each user found perform the tasks.

If the emails are the same for each user you could simply add all users to the distribution list - and send the mail once but this is only an option if you are sending the same email to everyone and there are no user specific per user settings.

Also depending on what the Instructions object does - you could structure your code either to reuse an existing instance or create a new one for each user.

My feeling though is that a foreach loop on a list of users is the way to go.
0
Ray PaseurCommented:
Design patterns are probably best understood by reading Martin Fowler.
http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420

But that said, design patterns are only high-level views of ways to solve common problems.  By the time you've reduced the ideas to the instantiation of  a class, you've already gotten deep into the problem, and well past the design phase.

PHP objects have both properties and methods.  Properties are pieces of data that are known to the methods.  Methods are programmatic functions that can operate on the data, and they are easily recognized because they end in parenthetical expressions, containing the arguments that are passed to the method.  Thus a statement like this has no meaning:

$user1->insert_phpmailer_errors;

If it were a method, it would look something like this:

$user1->insert_phpmailer_errors();

If it were to be used as a variable, it would look something like this:

$x = $user1->insert_phpmailer_errors;

Repeated tasks in PHP are performed with iterators.  Iterators are "control structures," so named because they control the flow of logic in the PHP script.  They include foreach() and while() and the other, less commonly used loop controls that are documented on those PHP.net pages.  Foreach() is commonly used with in-memory structures such as objects created from XML or JSON.  While() is commonly used with query results sets.
0
rgb192Author Commented:
>>My feeling though is that a foreach loop on a list of users is the way to go.

>>Repeated tasks in PHP are performed with iterators.  Iterators are "control structures," so named because they control the flow of logic in the PHP script.  They include foreach() and while()


should this be a factory where everything gets done for each username

because I think that the code will get disorganized with
if for foreach
statements
outside the class
0
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Ray PaseurCommented:
should this be a factory where everything gets done for each username
Possibly -- that would seem to make sense, but I can't really know for sure without seeing the data.  You would be a better judge of that than I can be.
0
Julian HansenCommented:
if for foreach
statements
outside the class
They have to exist somewhere - the point at which you put the iteration is highly dependent on the problem you are trying to solve.

If you create an encompassing class to encapsulate the interation - the iteration still exists in the same form it would anywhere else.

Basically KISS (keep it straight and simple) - often there is the urge to get overly complicated about solving a problem.

Two things should govern how you solve the problem.

Ease of coding
Ease of maintenance

A third is linked to the above which is extensibility always keep that in mind.

How to implement in your case requires more understanding of the problem you are trying to solve.
0
Slick812Commented:
since you give a mention for  "design pattern" , , I will say some about how POOR (not good) your code looks to me, First I am not sure about the parts (CLASSES) of this that you are writing, I will assume that you wrote or can modify the "Instructions" Class.

you show 4 methods that are called on every user -
$user1->select_rows_of_table_in_mysql($subject, $body, $email_addresses);
$user1->log_in_to_phpmailer($user_name);
$user1->send_emails_using_phpmailer($user_name);
$user1->insert_phpmailer_errors();

if you want a Better design pattern, then these 4 lines could be placed into a single method in the "Instructions" Class, maybe called mailerSend();
so you now have just ONE LINE of code to send a users email,
$user1->mailerSend($subject, $body, $email_addresses);

this IDEA of doing may lines of code in a Class method, instead of repetitively calling many lines in a loop or for each user, is the basic fundamental organization ADVANTAGE of Object programming, as this allows the developer to write many many less lines of code, saving time and effort.

you say some about - "should this be a factory where everything gets done for each username "
the way you have your code now I can NOT see any good way to use an object factory creation, as there are NO different factors that need to be used or placed in the Objects created for user1, user2, user3, It seems that the objects all do the same for every user.

I think I agree with your - "I think that I am copy pasting too many instances for each task "
so you may need to "Re-Think" your design pattern. Not sure about what the MAIN PURPOSE of the "Instructions" Class may be. If it is only for having a user-name send emails in phpmailer, then you might consider NOT having every user name be a condition for the object creation, and Then to send a phpmailer, you call methods with the user-name
$inst= new Instructions( );
$inst->select_rows_of_table_in_mysql($user_name, $subject, $body, $email_addresses);

then for your purposes here to send emails for multiple users, write a method that takes an array of user names, and an array of the subjects and address, maybe a sendMulti( ) method

$inst = new Instructions( );
$users = array('user1', 'user2', 'user3');
$emails= array(array('subject1','body1','joe@lo.com'), array('subject2','body2','mary@lo.com'), array('subject3','body3','jim@lo.com'));
$inst->sendMulti($users,  $emails);

just some ideas to consider
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
Thank you for teaching me ways to organize the code.

I will provide more examples soon
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.