Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object/scope question 1

Status
Not open for further replies.

gregmosu

Programmer
Jul 8, 2002
117
US
switching from java to php, and have a scope question. If I have a couple of classes that look like this:

Code:
<?php
class Person{
  //Some code that access the database
}
?>
<?php
class User{
  //Some code that access the database
}
?>

and I need to use them both in the same page, I'd use the require_once function to import both classes...

require_once('person.php');
require_once('user.php');

If both of these classes rely on a class called db_conn.php in order to connect to the database, then would I have to put require_once('db_conn.php'); in the page that is importing both of these classes, or could I put it in each of classes...

Code:
<?php
require_once('db_conn.php');
class Person{
  //Some code that access the database
}
?>
<?php
require_once('db_conn.php');
class User{
  //Some code that access the database
}
?>

Thanks,
Greg
 
Are the two classes Person and User related in any way? Is one a child of another?

If not, then each class file should include db_conn.php individually. The whole point of object-oriented code is to make code more portable for reuse, so an independent class take care of its own requirements seems the way to go.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
No, they're not related in any way. This is actually just a little test site so I can understand php a little better. But you're right, each class should have its own database connection. Putting the dbconn tag at the top of the index page and letting every class you include use it feels kinda like asp style coding.

Thanks,
Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top