rgb192
asked on
Assume that mam class is a black box new user class does not have access to database
followup to
https://www.experts-exchange.com/questions/28336179/Related-question-that-allowed-me-to-create-this-procedural-code-to-loop-through-all-the-users.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.
https://www.experts-exchange.com/questions/28336179/Related-question-that-allowed-me-to-create-this-procedural-code-to-loop-through-all-the-users.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.
ASKER
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).
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?
ASKER
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.
Can we see the mam class definition and see how you are using it, please?
ASKER
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
}
That gives us the usage. Can we also see the class definition, please? Thanks...
ASKER
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.
ASKER
What is class definition?
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.
Class mam { ...
It probably has class properties (variables) and methods (functions) inside it.
ASKER
I saw uml diagrams. So i will creat uml but make it in php. Or should i just copy paste whole class (many lines )?
I would avoid copying and pasting any code I did not completely understand!
ASKER
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
ASKER
Is this the proper way to write class definition?
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.
ASKER
could you give me example of function definitions.
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 */
}
}
ASKER
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
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
Maybe one reason you do not understand the code is because there is no code! These are all just empty method definitions.
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
so I need to focus on your other suggestions; Larry Ulman and Matt zandstra tutorial.
Thanks for guidence
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
All the best, ~Ray
ASKER
I will check.
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?