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

Thread Solved

Join Date: Sep 2009
Posts: 36
Reputation: Froger93 is an unknown quantity at this point 
Solved Threads: 4
Froger93 Froger93 is offline Offline
Light Poster

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

 
0
  #1
Oct 17th, 2009
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
  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
  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
  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. ?>
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso
 
0
  #2
Oct 17th, 2009
You have a few options.

You can send an instance to each class.
Ex.
  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.
Google is your friend.

Use [code] tags.

If you have found a solution to your problem, please mark the thread as SOLVED.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 878
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 141
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark
 
0
  #3
Oct 17th, 2009
$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).
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 36
Reputation: Froger93 is an unknown quantity at this point 
Solved Threads: 4
Froger93 Froger93 is offline Offline
Light Poster
 
0
  #4
Oct 17th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso
 
1
  #5
Oct 17th, 2009
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.
Google is your friend.

Use [code] tags.

If you have found a solution to your problem, please mark the thread as SOLVED.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 36
Reputation: Froger93 is an unknown quantity at this point 
Solved Threads: 4
Froger93 Froger93 is offline Offline
Light Poster
 
0
  #6
Oct 17th, 2009
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:
  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 .
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 36
Reputation: Froger93 is an unknown quantity at this point 
Solved Threads: 4
Froger93 Froger93 is offline Offline
Light Poster
 
0
  #7
Oct 17th, 2009
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
Reply With Quote Quick reply to this message  
Reply

Tags
class, methods, oop, php

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for class, methods, oop, php
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC