Solved

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

Posted on 2014-01-12
24
212 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
  • 13
  • 11
24 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:rgb192
Comment Utility
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 108

Expert Comment

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

Author Comment

by:rgb192
Comment Utility
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 108

Expert Comment

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

Author Comment

by:rgb192
Comment Utility
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
Comment Utility
What is class definition?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
I would avoid copying and pasting any code I did not completely understand!
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:rgb192
Comment Utility
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
Comment Utility
Is this the proper way to write class definition?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
could you give me example of function definitions.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
I will check.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now