944,124 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2262
  • PHP RSS
Oct 17th, 2009
0

OOP Help, Using another classe's function inside anther class

Expand Post »
Hey guys,

I have a problem. I have a connections_lib.php file this will handle all of my db connections, this works AMAZINGLY (power of OOP).

Unfortunately some of my other classes require connection to the databse therfore they must use these methods (is that the right word for OOP I'm new, or function whatever).

How would I go about doing this, I get the following error with this code:

Fatal error: Call to a member function start() on a non-object in C:\xampp\htdocs\project5\includes\members_lib.php on line 14

The scripts are ordered in the order they are shown below.

File: connections_lib.php
PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. class db {
  4. var $con;
  5. var $user;
  6. var $db;
  7. var $pass;
  8. var $host;
  9.  
  10. function __construct( $type ) {
  11. if( $type == "main" ) {
  12. $details = file_get_contents( $_SERVER['DOCUMENT_ROOT'] . "/includes/main.php" );
  13. $details = explode( "<?php /*" , $details );
  14. $details = $details[1];
  15. $details = explode( "*/ ?>" , $details );
  16. $details = $details[0];
  17. $details = explode( "-" , $details );
  18.  
  19. $this->user = $details[1];
  20. $this->pass = $details[2];
  21. $this->host = $details[3];
  22. $this->db = $details[0];
  23. }
  24. }
  25.  
  26. function start() {
  27. $this->con = mysql_connect( $this->host , $this->user , $this->pass ) or die( mysql_error() );
  28. mysql_select_db( $this->db , $this->con ) or die( mysql_error() );
  29. }
  30.  
  31. function close() {
  32. mysql_close( $this->con ) or die( mysql_error() );
  33. }
  34. }
  35.  
  36. ?>

File: members_lib.php
PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. class member {
  4. var $username;
  5. var $admin;
  6. var $first_name;
  7. var $last_name;
  8. var $email;
  9. var $reg_time;
  10. var $id;
  11.  
  12. function __construct( $id ) {
  13. if( isset( $id ) ) {
  14. $members->start();
  15. $query = mysql_query("SELECT * FROM `members` WHERE `id` = '" . mysql_real_escape_string( $id ) . "'");
  16. if( @mysql_num_rows( $query ) != 1 ) {
  17. return "<br /><strong>This user can not be found.</strong><br />";
  18. } else {
  19. $row = mysql_fetch_array( $query );
  20. $this->username = $row['username'];
  21. $this->first_name = $row['first_name'];
  22. $this->last_name = $row['last_name'];
  23. $this->email = $row['email'];
  24. $this->reg_time = $row['register_time'];
  25. $this->id = $id;
  26. }
  27. $members->close();
  28. } else {
  29. return "<br /><strong>You must submit an ID</strong><br />";
  30. }
  31. }
  32.  
  33. function setup( $id ) {
  34. if( isset( $id ) ) {
  35. $members->start();
  36. $query = mysql_query("SELECT * FROM `members` WHERE `id` = '" . mysql_real_escape_string( $id ) . "'");
  37. if( @mysql_num_rows( $query ) != 1 ) {
  38. return "<br /><strong>This user can not be found.</strong><br />";
  39. } else {
  40. $row = mysql_fetch_array( $query );
  41. $this->username = $row['username'];
  42. $this->first_name = $row['first_name'];
  43. $this->last_name = $row['last_name'];
  44. $this->email = $row['email'];
  45. $this->reg_time = $row['register_time'];
  46. $this->id = $id;
  47. }
  48. $members->close();
  49. } else {
  50. return "<br /><strong>You must submit an ID</strong><br />";
  51. }
  52. }
  53.  
  54. function get_reg_format( $format=false ) {
  55. if( $format == "DD-MM-YY" ) {
  56. return date( "d-m-y" , $this->reg_time );
  57. }
  58.  
  59. if( $format == "DAY-MONTH-YEAR" ) {
  60. return date( "l jS F Y" , $this->reg_time );
  61. }
  62. }
  63.  
  64. function update( $field , $new_value ) {
  65. $members->start();
  66. $tables = mysql_query("SHOW TABLES");
  67. while( $row = mysql_fetch_array( $tables ) ) {
  68. $columns = mysql_query("SHOW COLUMNS FROM `" . $row[0] . "`");
  69. while( $row2 = mysql_fetch_array( $columns ) ) {
  70. if( $field == $row2[0] ) {
  71. $table .= $row[0];
  72. $update = mysql_query("UPDATE `" . $table . "` SET `" . $field . "` = '" . mysql_real_escape_string( $new_value ) . "' WHERE `id` = '" . mysql_real_escape_string( $this->id ) . "'");
  73. $query_done++;
  74. }
  75. }
  76. }
  77.  
  78. if( $query_done == false ) {
  79. return false;
  80. } else {
  81. return true;
  82. $this->setup($this->$id);
  83. }
  84. $members->close();
  85. }
  86. }
  87.  
  88. ?>

File: admin_lib.php
PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. class admin {
  4. var $level;
  5. var $color;
  6. var $level_name;
  7. var $username;
  8.  
  9. function __construct( $id ) {
  10. $admin->start();
  11. if( cookie_set() ) {
  12. $query = mysql_query("SELECT * FROM `admin_members` WHERE `id` = '" . mysql_real_escape_string( $id ) . "'");
  13. if( @mysql_num_rows( $query ) != 1 ) {
  14. $add_level = mysql_query("INSERT INTO `admin_members` (id, auth_level) VALUES ('" . mysql_real_escape_string( $id ) . "', '1')");
  15. $query = mysql_query("SELECT * FROM `admin_members` WHERE `id` = '" . mysql_real_escape_string( $id ) . "'");
  16. }
  17. $auth = mysql_fetch_array( $query );
  18. $query_level_dets = mysql_query("SELECT * FROM `admin_levels` WHERE `id` = '" . mysql_real_escape_string( $auth['auth_level'] ) . "'");
  19. $auth2 = mysql_fetch_array( $query_level_dets );
  20. $this->level = $auth['auth_level'];
  21. $this->color = $auth2['color'];
  22. $this->level_name = $auth2['level_name'];
  23. $this->id = $id;
  24. }
  25. $admin->close();
  26. }
  27.  
  28. function get_formatted_username( $username , $link=false ) {
  29. if( $link == true ) {
  30. return "<a href=\"" . $site . "members/view.php?id=" . urlencode( $username ) . "\" style=\"color: " . $this->color . "\">" . $username . "</a>";
  31. } else {
  32. return "<span style=\"color: " . $this->color . "\">" . $username . "</span>";
  33. }
  34. }
  35.  
  36. function get_formatted_admin() {
  37. return "<span style=\"color: " . $this->color . "\">" . $this->level_name . "</span>";
  38. }
  39.  
  40. function isStaff() {
  41. if( $this->level_name == "Staff" ) {
  42. return true;
  43. } else {
  44. return false;
  45. }
  46. }
  47.  
  48. function isW3hut() {
  49. if( $this->level_name == "W3Hut" ) {
  50. return true;
  51. } else {
  52. return false;
  53. }
  54. }
  55. }
  56.  
  57. ?>
Similar Threads
Reputation Points: 11
Solved Threads: 6
Junior Poster in Training
Froger93 is offline Offline
74 posts
since Sep 2009
Oct 17th, 2009
0
Re: OOP Help, Using another classe's function inside anther class
You have a few options.

You can send an instance to each class.
Ex.
PHP Syntax (Toggle Plain Text)
  1. $db = new db;
  2. $admin = new admin( $db,$id ); //send the db object to the class

Or use the singleton method. Look this one up as it wouldn't be easy to show you an example that you could understand.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Oct 17th, 2009
0
Re: OOP Help, Using another classe's function inside anther class
$members->start();

This requires that $members is an object, created with:

$members = new <classname>;

Where <classname> is the class you want it to be (probably db).
Sponsor
Featured Poster
Reputation Points: 557
Solved Threads: 735
Bite my shiny metal ass!
pritaeas is offline Offline
4,204 posts
since Jul 2006
Oct 17th, 2009
0
Re: OOP Help, Using another classe's function inside anther class
Thanks for the reply.

Is there a way I can do this without having to input a connection object to the admin function?

I'd rather the admin class connects it's self. I have just though of another alternative that may work, could I extend the db class and then use the functions from db?
Reputation Points: 11
Solved Threads: 6
Junior Poster in Training
Froger93 is offline Offline
74 posts
since Sep 2009
Oct 17th, 2009
1
Re: OOP Help, Using another classe's function inside anther class
You could extend it, but every time you called a class a new connection to the database would be made (which isn't good).

I would use the singleton method.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Oct 17th, 2009
0
Re: OOP Help, Using another classe's function inside anther class
Okay, so could I create the method start() again but use it under admin as it is it's own method? This way it would only connect if the object is used.

As you know I store my connection details in a file in the format:
PHP Syntax (Toggle Plain Text)
  1. <?php /*database-username-password-host*/ ?>

So I could just perform the same operations on that as I did for the db object?

Is it possible to use an objects methods as part of another method inside the same object?

@pritaeas - Thanks for the reply also, I did declare the object inside these documents but got unexpected function so I moved the decleration outside of the file and used it before I included the document.

Thanks for the help so far guys .
Reputation Points: 11
Solved Threads: 6
Junior Poster in Training
Froger93 is offline Offline
74 posts
since Sep 2009
Oct 17th, 2009
0
Re: OOP Help, Using another classe's function inside anther class
Okay I used what I just described and this seemed to work seemlesley so I have solved my problem.

I would still like to hear back from you if thats okay to let me know if this will infact only connect if a new object is created?

Thanks
Reputation Points: 11
Solved Threads: 6
Junior Poster in Training
Froger93 is offline Offline
74 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: MySql Post
Next Thread in PHP Forum Timeline: View profile





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC