Jump to content

Class Usage


Liquid Fire

Recommended Posts

I don't know if it is just the code i have worked on or what but the class usage I have sent in php seem weird.  Most of the time the classes do not store any variables and if they do it is just a database object.  Like i have been a user class the has a get method that return and array the is return from the database class instead of taking the database and storing the array in class members by modifing the class the called the get method.  Id this very common and if so,  why?  Is this something you would do?

 

I will admit i do have a class like this but only have one class and really only using it as a namespace and using if for functions that are site specific.

Link to comment
Share on other sites

I don't understand this:

Like i have been a user class the has a get method that return and array the is return from the database class instead of taking the database and storing the array in class members by modifing the class the called the get method.

 

???

Link to comment
Share on other sites

sorry about that, how about let people edit post so i don't have to repost them.

 

 

I don't know if it is just the code I have worked on or what but the class usage I have seen in php seems weird.  Most of the time the classes do not store any variables and if they do it is just a database object.  Like, I have seen a user class the has a get method that return an array that is returned from the database class call instead of taking the database returned array and storing it in classes members.  Is this very common and if so, why?  Is this something you would do?

 

I will admit I do have a class like this but only have one class per site and really only using it as a namespace and using if for functions that are site specific.

 

Link to comment
Share on other sites

  • 4 weeks later...

I do not understand exactly either..

 

but is your question about "layers" in architecture.

 

like, to get data, we would instance a class and call a method, which in turn instances a data access class, does the required and returns the value. So it looks like we could have actually directly used the methods in the data class.

 

is that your question?

 

if yes,

layering implementation as "front-end classes > logic classes > database classes" make more sense than doing it directly in bigger projects .. project which have or will easily grow to have more than 3000 lines of code.

 

since i am not sure whether this addresses your question, i will like to explain more if this helped.

 

regards,

Harish

www.floresense.com

www.harishpalaniappan.com

Link to comment
Share on other sites

Depends on if they need to be stored or not.  I use a $lastResult variable in my database class to store results from a query before I return them.  But I don't store user info inside the user class -- just the basics when the object is created.  I don't need to know their email, or their AIM handle, inside the class.  Just return the array.  I do however store the User ID, since it is the key for the user account in my database.  The most common one for a user class is $is_logged or whatever -- are they logged in or not?

Link to comment
Share on other sites

I think I will have to agree with you on people creating classes which just continue to interact with a database to fetch the data, even if they have fetched it before. I prefer to limit the amount of times I need to get information from the database.

 

Bad Example:

A Class with:

- GetUser (returns array) function

- Login (returns array) function

- UserExists function

- Valid Password function

 

Login checks if user exists, if user exists, it then gets the user, then it uses valid password to check the password. So really its checking the DB twice, and jumping all around for something which isnt needed

 

 

Good Example in my opinion

A user class with

- __construct($value='',$key='userid') (and using get_num_funcs() to determine if any params passed) - Storing in private var

- Login function which is designed to be used statically User::Login() which handles all login process and returna a new User object for the user

- Use __get and __set to interact with the stored variable of user data

- Has a commit function to store updated user or create a new user.

 

I hate to see code which is inefficiant, or just plain stupid.

 

e.g. $q = mysql_query('SELECT * FROM users WHERE username="x"') or die(mysql_error());

while($row = mysql_fetch_array($q))

$user = $row;

 

Optimised

 

$q = mysql_query('SELECT * FROM users WHERE username="x"') or die(mysql_error())

$user = mysql_fetch_assoc($q); // Only one row returned, and only ever going to use assco never indexs.

 

People need to learn to optimise thats all it is

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.