Avatar of rgb192
rgb192
Flag for United States of America 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.
PHP

Avatar of undefined
Last Comment
rgb192

8/22/2022 - Mon
Ray Paseur

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?
rgb192

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).
Ray Paseur

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?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
rgb192

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.
Ray Paseur

Can we see the mam class definition and see how you are using it, please?
rgb192

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
  }

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

That gives us the usage.  Can we also see the class definition, please?  Thanks...
rgb192

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.
rgb192

ASKER
What is class definition?
Your help has saved me hundreds of hours of internet surfing.
fblack61
Ray Paseur

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.
rgb192

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 )?
Ray Paseur

I would avoid copying and pasting any code I did not completely understand!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
rgb192

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
	

Open in new window

rgb192

ASKER
Is this the proper way to write class definition?
Ray Paseur

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
rgb192

ASKER
could you give me example of function definitions.
Ray Paseur

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

rgb192

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
	

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

Maybe one reason you do not understand the code is because there is no code!  These are all just empty method definitions.
rgb192

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
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rgb192

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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Ray Paseur

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
rgb192

ASKER
I will check.