| | |
OOP Help, Using another classe's function inside anther class
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 36
Reputation:
Solved Threads: 4
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
File: members_lib.php
File: admin_lib.php
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)
<?php class db { var $con; var $user; var $db; var $pass; var $host; function __construct( $type ) { if( $type == "main" ) { $details = file_get_contents( $_SERVER['DOCUMENT_ROOT'] . "/includes/main.php" ); $details = explode( "<?php /*" , $details ); $details = $details[1]; $details = explode( "*/ ?>" , $details ); $details = $details[0]; $details = explode( "-" , $details ); $this->user = $details[1]; $this->pass = $details[2]; $this->host = $details[3]; $this->db = $details[0]; } } function start() { $this->con = mysql_connect( $this->host , $this->user , $this->pass ) or die( mysql_error() ); mysql_select_db( $this->db , $this->con ) or die( mysql_error() ); } function close() { mysql_close( $this->con ) or die( mysql_error() ); } } ?>
File: members_lib.php
PHP Syntax (Toggle Plain Text)
<?php class member { var $username; var $admin; var $first_name; var $last_name; var $email; var $reg_time; var $id; function __construct( $id ) { if( isset( $id ) ) { $members->start(); $query = mysql_query("SELECT * FROM `members` WHERE `id` = '" . mysql_real_escape_string( $id ) . "'"); if( @mysql_num_rows( $query ) != 1 ) { return "<br /><strong>This user can not be found.</strong><br />"; } else { $row = mysql_fetch_array( $query ); $this->username = $row['username']; $this->first_name = $row['first_name']; $this->last_name = $row['last_name']; $this->email = $row['email']; $this->reg_time = $row['register_time']; $this->id = $id; } $members->close(); } else { return "<br /><strong>You must submit an ID</strong><br />"; } } function setup( $id ) { if( isset( $id ) ) { $members->start(); $query = mysql_query("SELECT * FROM `members` WHERE `id` = '" . mysql_real_escape_string( $id ) . "'"); if( @mysql_num_rows( $query ) != 1 ) { return "<br /><strong>This user can not be found.</strong><br />"; } else { $row = mysql_fetch_array( $query ); $this->username = $row['username']; $this->first_name = $row['first_name']; $this->last_name = $row['last_name']; $this->email = $row['email']; $this->reg_time = $row['register_time']; $this->id = $id; } $members->close(); } else { return "<br /><strong>You must submit an ID</strong><br />"; } } function get_reg_format( $format=false ) { if( $format == "DD-MM-YY" ) { return date( "d-m-y" , $this->reg_time ); } if( $format == "DAY-MONTH-YEAR" ) { return date( "l jS F Y" , $this->reg_time ); } } function update( $field , $new_value ) { $members->start(); $tables = mysql_query("SHOW TABLES"); while( $row = mysql_fetch_array( $tables ) ) { $columns = mysql_query("SHOW COLUMNS FROM `" . $row[0] . "`"); while( $row2 = mysql_fetch_array( $columns ) ) { if( $field == $row2[0] ) { $table .= $row[0]; $update = mysql_query("UPDATE `" . $table . "` SET `" . $field . "` = '" . mysql_real_escape_string( $new_value ) . "' WHERE `id` = '" . mysql_real_escape_string( $this->id ) . "'"); $query_done++; } } } if( $query_done == false ) { return false; } else { return true; $this->setup($this->$id); } $members->close(); } } ?>
File: admin_lib.php
PHP Syntax (Toggle Plain Text)
<?php class admin { var $level; var $color; var $level_name; var $username; function __construct( $id ) { $admin->start(); if( cookie_set() ) { $query = mysql_query("SELECT * FROM `admin_members` WHERE `id` = '" . mysql_real_escape_string( $id ) . "'"); if( @mysql_num_rows( $query ) != 1 ) { $add_level = mysql_query("INSERT INTO `admin_members` (id, auth_level) VALUES ('" . mysql_real_escape_string( $id ) . "', '1')"); $query = mysql_query("SELECT * FROM `admin_members` WHERE `id` = '" . mysql_real_escape_string( $id ) . "'"); } $auth = mysql_fetch_array( $query ); $query_level_dets = mysql_query("SELECT * FROM `admin_levels` WHERE `id` = '" . mysql_real_escape_string( $auth['auth_level'] ) . "'"); $auth2 = mysql_fetch_array( $query_level_dets ); $this->level = $auth['auth_level']; $this->color = $auth2['color']; $this->level_name = $auth2['level_name']; $this->id = $id; } $admin->close(); } function get_formatted_username( $username , $link=false ) { if( $link == true ) { return "<a href=\"" . $site . "members/view.php?id=" . urlencode( $username ) . "\" style=\"color: " . $this->color . "\">" . $username . "</a>"; } else { return "<span style=\"color: " . $this->color . "\">" . $username . "</span>"; } } function get_formatted_admin() { return "<span style=\"color: " . $this->color . "\">" . $this->level_name . "</span>"; } function isStaff() { if( $this->level_name == "Staff" ) { return true; } else { return false; } } function isW3hut() { if( $this->level_name == "W3Hut" ) { return true; } else { return false; } } } ?>
0
#2 Oct 17th, 2009
You have a few options.
You can send an instance to each class.
Ex.
Or use the singleton method. Look this one up as it wouldn't be easy to show you an example that you could understand.
You can send an instance to each class.
Ex.
PHP Syntax (Toggle Plain Text)
$db = new db; $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.
Use [code] tags.
If you have found a solution to your problem, please mark the thread as SOLVED.
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.
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.
Use [code] tags.
If you have found a solution to your problem, please mark the thread as SOLVED.
•
•
Join Date: Sep 2009
Posts: 36
Reputation:
Solved Threads: 4
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:
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
.
As you know I store my connection details in a file in the format:
PHP Syntax (Toggle Plain Text)
<?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
. ![]() |
Similar Threads
- use a function inside a class (C++)
- Main function inside the class ? (Java)
- calling a function inside that same function (C++)
- Calling a member function inside a class (C++)
- Is It significant to define a member function as static in singleton class (C++)
- Problem on calling a javascript in a class (ASP.NET)
- Bank account class (C++)
Other Threads in the PHP Forum
- Previous Thread: MySql Post
- Next Thread: View profile
| Thread Tools | Search this Thread |
Tag cloud for class, methods, oop, php
ajax api array asp beginner c# c++ class cms code codes combobox curl data database date delete display downloader dropdownlist elearning email error errors external file files flash form forms function functions graph hosting html iframe image include integration java javascript jquery js lamp libcurl limit link linux login mail malfunction menu method methods mssql multiple mysql navigation nodes object oop paypal php popup post printer projectmanagement query recursiveloop reference script search security server session sms soap spam sql static subclass survey system tutorial undefined upload validation variable vbulletin video virus web webbrowser webdesign websphere xml xslt yahoo youtube zend







