Jump to content

Maintaining a Class (not sure how to ask the question o_o)


kratsg

Recommended Posts

Let's say I have some sort of a user login management website of sorts. General idea would be different pages, profiles, login, logout, register, etc...

 

So a user logs in, I call a class to validate this user. I use another class (class User) to fetch all the data for that particular user, set it into a mega array such as $user["id"] or $user["email"] to store all data to be called later.

 

Page1.php

include "someuserclass.php"
$user = new User;
$user->setuser($username);
$user->compile_array();//sets $user to contain the array that is returned by the class function
//from this point, I can easily call data, etc...

 

Now, wait. Let's say I go to Page2.php, does this mean I have to re-initiate the class, do the same thing as I did for Page1.php (with the include, initiate class, setuser, and then compile data)? Is there some way to maintain the class over different pages (as you would do with cookies or sessions, without actually using cookies or sessions)? The only way that I can think of doing this is to include this in the "head.php" page which contains the header stuff, like top of the layout, title, etc... like so:

 

Head.php

include "someuserclass.php"
include "somerequiredfunctions.php";
include "database_conn.php";
$user = new User;
$user->setuser($username);
$user->compile_array();//sets $user to contain the array that is returned by the class function
//insert some html here

 

Page1.php

include "Head.php";
//do other stuff here, etc..

 

Page2.php

include "Head.php";
//do some other stuff here...

 

My biggest worry is that since I'm fetching data from the database, that this makes a NEW call to the database for each page that I load (to get user data). Is there anyway to maximize efficiency, and cut-down on server usage/memory? [hopefully, something that may include a mysql_free_result() function, which I don't know WHEN to use it, or why you would use it if it clears your variables...]

Link to comment
Share on other sites

afaik you have to either use sessions/cookies or call that method every time. Also you should use 'require_once' and 'include_once' to avoid declaring methods twice.

 

And on the MySQL thing you'll just have to close the connection each time when you're done with it, although PHP closes those by itself after script execution is done.

Link to comment
Share on other sites

I see... So a mysql_close will do what I need.

 

For website integration and management with OOP, do you suggest having a class for the database, so that on each page, you include this class, run a function to open database connection, select database, and for queries? And then, just have multiple classes for user checking access, run the function to get user access and compare it to page access, etc...?

 

Like:

 

mysql class

$q = new Mysql;
$q->connect();
$q->select("some database");
@mysql_query("do some stuff blah blah");

 

Or would I be better off with:

$q = new Mysql;
$q->connect();
$q->select("some database");
$q->update("table","column","new value","where this","equals this");

 

I kinda prefer the first one, as you're able to have more customizability, but in terms of efficiency, which would be preferred?

 

Then for user checking:

$user = new User;
$access = new Access;
$access->access = true;
$access->user_access = $user->access;
$access->check_access();

 

It seems a bit simple for me to do it this way, instead of if/else statements on so many pages. Does anyone think this would be the best method for checking access using classes (assuming perfected functions).

Link to comment
Share on other sites

$q = new Mysql;
$q->connect();
$q->select("some database");

 

Better

 

$q = new Mysql;
$q->connect("some database");

 

And your user checking is very, very bizarre. What is the meaning of class Access? I presume all functionality should be located in class User.

 

$user = new User("id"); // or "nick" or whatever
$user->CheckAccess(); // but it could be already done in constructor
...
if ($user->WritingAllowed) ...

 

Link to comment
Share on other sites

$user = new User("id"); // or "nick" or whatever
$user->CheckAccess(); // but it could be already done in constructor
...
if ($user->WritingAllowed) ...

 

Not quite sure how a constructor class is set-up (I know its something about running once the class is called).

 

How do you pass the "id" variable into the class?

 

I was gonna have a method of accessing the database to get the filename of the script the user is access, check it's access level against the user's access level inside the class (let me know if this is the best option).

Link to comment
Share on other sites

Huh, it should be of course

 

$user = new User($id);

 

if this is the best option

 

Well, it depends on global architecture of your application. You can include script in independent files, you can have "nice URLs" and dispatch actions internally from URL parts ... there is no best option. IMHO.

Link to comment
Share on other sites

freeing the result will have no effect since if you close the database handle, the memory used for this is flushed.

You should only free the result if you do a HUGE query and after that you want to do another (huge) query using the same database handle.

That is misleading information. Semantically, you should free the result set when you have finished with it. It is not essential, however.

 

P.S. if my site has over 1000 hits an hour, there will be a noticeable increase in performance when the memory is freed from every result set as soon as possible, compared to leaving the Zend engine to do it for me.

Link to comment
Share on other sites

Constructors are done inside the class and get executed upon "construction" of the class.

i.e. $class = new myClass(); calls the constructor of the class.

 

The code for such is :

class myClass {
  // This is the constructor.
  public function __construct(){

  }

  // This is the constructor for <= PHP4
  public function myClass() {

  }

}

 

If you want backward compatibility have the myClass() constructor called by the __construct() constructor, and put all your construction code inside the myClass() function.

Link to comment
Share on other sites

  • 3 weeks later...
$q = new Mysql;

$q->connect();

$q->select("some database");

 

I have a quick question. I have a Database Class like the OP. Now I initialize the class in the class file itself. My class file looks kinda like this

Class MySQL 
     {
        // Necessary code goes here
     }

$db = new MySQL();

 

Now is this a bad practice? I got it from a tutorial that did it this way and I thought it won't be so bad to have the class initialized already so I can just use it immediately without have to write the

 

$db = new MySQL();

 

In every page that uses it.

Link to comment
Share on other sites

The only bad thing about that approuch would be that any time you need to change host,username, pass or database you actually need to change the code within the class.

 

A better option would be to have your class read from some external config file, this way you only need to change the config file and not your code.

Link to comment
Share on other sites

The only bad thing about that approach would be that any time you need to change host,username, pass or database you actually need to change the code within the class.

 

A better option would be to have your class read from some external config file, this way you only need to change the config file and not your code.

 

Yeah..I have an config file that is included before the class where the  information of host username etc

 

include 'config';

Class MySQL 
     {
        // Necessary code goes here
     }

$db = new MySQL();

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.