[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Assume that mam class is a black box new user class does not have access to database

Posted on 2014-01-12
24
Medium Priority
?
231 Views
Last Modified: 2014-03-24
followup to

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

Assume that mam class is a black box
new user class does not have access to database


mam class has some methods that allow me to run instance queries in index.php

there is no customized method in mam class that loops using foreach (array or select table)

I can not run queries without a method from mam class

I do not want to edit mam class because whenever programmer makes edit: sends new class to me (which will not have my old edits)


Is there a way to create user class with instance of hardcoded usernames and passwords using object oriented code with no database connection.
0
Comment
Question by:rgb192
[X]
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
  • 13
  • 11
24 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39775328
Is there a way to create user class with instance of hardcoded usernames and passwords using object oriented code with no database connection.
Yes, but in the context of a real-world computer science question, it does not make any sense.  The general practice of object-oriented programming is designed to reduce dependencies on code and increase reusability.  If you have hard-coded usernames and passwords, you've lost both of those features.

You can write a class with a collection of hard-coded usernames and associated passwords.  You might make these constants or assign them in the class constructor.  But what would this class be useful for?
0
 

Author Comment

by:rgb192
ID: 39775350
You can write a class with a collection of hard-coded usernames and associated passwords.  You might make these constants or assign them in the class constructor.  But what would this class be useful for?

Only the instance would be hard coded.

The code would look alittle nicer than the procedural for loop I have now.
Hopefully I will learn more and then there will be a refactor or rewrite to your desired outcome (a user class with database access).
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39776314
Only the instance would be hard coded.
In object-oriented programming, instance is a term of art.  In PHP terminology, it is the object that is made from a class definition, when you instantiate the object by running the class constructor, using the keyword "new."  As such, it exists only in memory, and not on paper.  In other words, it cannot be hard coded because it does not exist until after the PHP interpreter has begun to run the PHP instructions in the script.

A class can be hard-coded.  But that would probably only be done if you were writing an abstract class and had plans to extend it with working methods.

Are you working on some kind of online example or tutorial that you can tell us about?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rgb192
ID: 39776792
Are you working on some kind of online example or tutorial that you can tell us about?

I am working on code which gets updated another programmer which is not a tutorial.

I test code and give suggestions for the programmer.

I test on the usernames.

So there could be a way to extend mam class just to get a method that has the pdo but I think it is too difficult for me.

A class can be hard-coded.  But that would probably only be done if you were writing an abstract class and had plans to extend it with working methods.

There are no abstract classes or interfaces that set rules for the methods that are required in mam class.  I was taught by ee that interfaces are rules when there are 5 or more programmers.


The current procedural foreach loop works for 4 usernames, but there is a 5th username which has a different password which requires me to alter procedural user foreach so I need to delete and run again.  So I need to run the code twice to loop through 5 usernames.


This is not production ready code yet. I am just testing what another programmer creates.  I agree with you, when there are many users and those users change their password by themselves, then the passwords should be stored in plaintext so I can use somewhere else or passwords encrypted so only called from the database.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39777216
Can we see the mam class definition and see how you are using it, please?
0
 

Author Comment

by:rgb192
ID: 39777456
http://mobile.experts-exchange.com/viewCodeSnippet.jsp?refID=28336179&rtid=10&icsi=1

// 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

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39777472
That gives us the usage.  Can we also see the class definition, please?  Thanks...
0
 

Author Comment

by:rgb192
ID: 39777477
Where $config is array defined in config file.  Config Values are username, password, timezone,  language, db type, db hostname, db username, db name, db password. But i can set instance properties again before running a method.
0
 

Author Comment

by:rgb192
ID: 39777577
What is class definition?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39777620
It's the part of the code that starts with something like this:

Class mam { ...

It probably has class properties (variables) and methods (functions) inside it.
0
 

Author Comment

by:rgb192
ID: 39816298
I saw uml diagrams.  So i will creat uml but make it in php. Or should i just copy paste whole class (many lines )?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39816422
I would avoid copying and pasting any code I did not completely understand!
0
 

Author Comment

by:rgb192
ID: 39857814
class mam
{
	public $dbh = NULL;

	public $default_username = '';
	public $default_password = '';
	public $action = array();
	public $chosen_action = 0;
	public $log_file = '';
	public $db_type = '';
	public $db_hostname = '';
	public $db_port = '';
	public $db_name = '';
	public $db_username = '';
	public $db_password = '';


	public function __construct($config = array())
	
	public function doAction()
	//case statements to call class functions
	//class functions can also be called outside class by creating object
	
	public funciton deleteMessage($default_username = '', $arr_profile_id_or_sql = array(), $arr_sql_params = array())
	
	public function ConvertDateTime($date_time_str = 'now')
	//Ray wrote this converting written time to mysql time (so I do not need varchar column, I can use mysql datetime
	
	public function EchoMessage($default_username = '')
	//saves message into the database
	
	public function ProfileName($default_username = '', $arr_profession = array())
	//lists profile_id and profession but does not save information to database yet
	
	public function ParseProfile($default_username = '', $arr_profile_id_or_sql = array(), $arr_sql_params = array())
	//gathers attributes about profile_id and stores to database
	
	public function ParseProfile2($default_username = '', $arr_profile_id_or_sql = array(), $arr_sql_params = array())
	//updated class of ParseProfile
	//gathers attributes about profile_id and stores to database
	

Open in new window

0
 

Author Comment

by:rgb192
ID: 39884774
Is this the proper way to write class definition?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39885266
It may be a place to start, but it should be expected to fail with a parse error since it does not have the control structure curly braces required for the function definitions.
0
 

Author Comment

by:rgb192
ID: 39890966
could you give me example of function definitions.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39891657
It would look something like this
<?php
error_reporting(E_ALL);
class Thing
{
    public function __construct($config = array())
    {
        /* CODE FOR THE CONSTRUCTOR HERE */
    }
    public function doAction()
    {
        /* CODE FOR DOACTION() HERE */
    }
}

Open in new window

0
 

Author Comment

by:rgb192
ID: 39911428
class mam
{
	public $dbh = NULL;

	public $default_username = '';
	public $default_password = '';
	public $action = array();
	public $chosen_action = 0;
	public $log_file = '';
	public $db_type = '';
	public $db_hostname = '';
	public $db_port = '';
	public $db_name = '';
	public $db_username = '';
	public $db_password = '';


	public function __construct($config = array()){
      //does something with a cookie of which I do not understand
}
	
	public function doAction(){
	//case statements to call class functions
	//class functions can also be called outside class by creating object
}	

	public funciton deleteMessage($default_username = '', $arr_profile_id_or_sql = array(), $arr_sql_params = array()){
}
	
	public function ConvertDateTime($date_time_str = 'now'){
	//Ray wrote this converting written time to mysql time (so I do not need varchar column, I can use mysql datetime
}	
	public function EchoMessage($default_username = ''){
	//saves message into the database
	}
	public function ProfileName($default_username = '', $arr_profession = array()){
	//lists profile_id and profession but does not save information to database yet
	}
	public function ParseProfile($default_username = '', $arr_profile_id_or_sql = array(), $arr_sql_params = array()){
	//gathers attributes about profile_id and stores to database
	}
	public function ParseProfile2($default_username = '', $arr_profile_id_or_sql = array(), $arr_sql_params = array()){
	//updated class of ParseProfile
	//gathers attributes about profile_id and stores to database
}
}//close class
	

Open in new window


I would avoid copying and pasting any code I did not completely understand!
I really dont understand the code but I can call the methods and set properties
and I can see database queries using mysql workbench
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39912516
Maybe one reason you do not understand the code is because there is no code!  These are all just empty method definitions.
0
 

Author Comment

by:rgb192
ID: 39939960
There is code but you told me to not copy and paste any code I did not understand.  I understand empty methods, but not the data in the methods.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39940372
Uhh, OK.  Let me try to clarify.

When you go online and look for code samples, especially with PHP, you always run the risk that the online code samples are out of date, insecure or just plain wrong.  There are a lot of people who "dabble" in PHP without any real understanding of the principles of computer science.  Sometimes they leave code in forums, blog posts, etc.  There is zero quality control on this stuff, and it's often left around for years.  Even the user-contributed notes on PHP.net are sometimes off the mark or out of date.  Therefore I try to dissuade anyone from picking up code and using it without a careful reading to develop an understanding of what it does.

It's not about pasting code here at EE, it's about pasting code into your applications.
0
 

Author Closing Comment

by:rgb192
ID: 39948777
I do not understand the code, so I need to follow your advice and avoid using php from "online code samples are out of date, insecure or just plain wrong"

so I need to focus on your other suggestions; Larry Ulman and Matt zandstra tutorial.

Thanks for guidence
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39949019
Smart to stick to dependable sources!  Ullman will be speaking at the upcoming PHP Architect conference.  Worth Googling it to see if you want to attend.

All the best, ~Ray
0
 

Author Comment

by:rgb192
ID: 39950668
I will check.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

649 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