Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Related question that allowed me to create this procedural code to loop through all the users

Posted on 2014-01-11
8
255 Views
Last Modified: 2014-01-12
Related question that allowed me to create this procedural code to loop through all the users

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28315794.html

// next create your Name Array
$input = array('user1','anotherUser', 'anotherUser2','bob');

foreach ($input as $uName) {
//from a config file. I decided to create object this each time to initiate the constructor
  $m_instance = new mam($config);
  $m_instance->default_username=$uName;
// works if all the users have 'password' as their password
  $m_instance->default_password='password';
  $m_instance->method();
//just to make sure that the instance is destructed
  unset($m_instance);
// needs } at the end of code to close the foreach
  }

Open in new window



problem is the code is procedural and I need to close the foreach bracket } at the end of code
and if password changes, it is hard coded

I want an object oriented class to do this.
0
Comment
Question by:rgb192
  • 4
  • 4
8 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39773758
It looks to me like what you're doing here makes sense.  I'm not sure why you would want to change it.  The only thing that might change is that you would make the programming more complicated, and that's kind of the opposite of the objective of object oriented design, which is intended to make the programming simpler.

OOP and procedural code coexist beautifully, and it's the programmer's task to know what tool to use for the job at hand.  Sometimes you need a hammer, sometimes you need a wrench.

What you might consider is the use of setter() and getter() functions in concert with protected properties.  This would be better form than just injecting properties into the objects.  Good description and example here:
http://php.net/manual/en/language.oop5.visibility.php#108711
0
 

Author Comment

by:rgb192
ID: 39774002
I think there should be a class with a constructor that accepts username and password.

Because if one user decides to change their password, this code requires if statements


I do not understand how setter and getter would work in this example

maybe set the username, password
and have the $mam class get the username,password

but I do not want to change the $mam class because I did not create it and class already works
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39774021
... a class with a constructor that accepts username and password
That makes sense to me.  The class would probably look up the user record (based on the username and password) and load the user record into memory.  Perhaps it would store the user record information in the PHP session.  There could be a lot of user information.  For example you could connect the user to her viewing and purchase history, locale, and local business promotions.  You could create a social graph of friends.  All of these things could be created in the object at instantiation.

If() statements are not an evil.  They are an indispensable tool to control the flow of logic.

$mam would make sense as a variable name, but the class name would not have a dollar-sign.

Where is the Mam class defined?  Is this part of a tutorial or classroom lesson?  It's not familiar to me.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:rgb192
ID: 39774985
Where is the Mam class defined?  Is this part of a tutorial or classroom lesson?  It's not familiar to me.

custom code that interacts with our existing data to interact with users

If() statements are not an evil.  They are an indispensable tool to control the flow of logic.

I would prefer object oriented class so I could learn, how can username and pass be initialized if the password is different for every user

$input = array('user1','anotherUser', 'anotherUser2','bob');
$usernamePassword1 = array('user1','pass'),
$usernamePassword2 = array('anotherUser2','passAnother'),
$usernamePassword3 = array('anotherUser2','pass2'),
$usernamePassword4 = array('bob',bobpass');
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39775001
Each different "user" (however that term is defined) would be represented by a unique object instance of some kind of "user" class.

What would you have us make of the code here?
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28336179.html#a39774985
0
 

Author Comment

by:rgb192
ID: 39775130
Each different "user" (however that term is defined) would be represented by a unique object instance of some kind of "user" class.
okay the user class will have a constructor with two variables username and password


What would you have us make of the code here?
I created an example where each username had a different password.

I do not even understand how to do this with procedural if
maybe procedural case
and I think that there will be copy paste errors as new users are added.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39775148
... will be copy paste errors as new users are added
This sounds like we are not getting the bigger picture.  Nobody adds "users" in any application by copying and pasting.  The usual design pattern is to store the user information in a data base table.  A cookie would be placed on the browser which was a key to the appropriate row of the table, and this would be used to retrieve the user information.  If the cookie was not present, the script would present a form to receive the username and password.  When the form is submitted, the script would try to find the username and password in the data base, and if found would place the cookie on the browser.  If not found, the login would be denied.
0
 

Author Closing Comment

by:rgb192
ID: 39775205
database connection is an answer because only mam class has access to database
(does not work for me personally)

thanks


related question:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28336597.html
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question