Forum Moderators: coopster

Message Too Old, No Replies

Access to a class from inside another

How should i proceed?

         

FnTm

3:11 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



Hi!

I have decided to write my own authorization class. So, I allready have MySQL class that I use for all of my MySQL needs. But now I need to access some of the functions from MySQL class while running my Auth class.

auth.class.php and mysql.class.php files are located in a directory on my webserver.

How do I access lets say: $mysql->insert_function() from my $auth->register_user() function?

Thanks!

coopster

3:40 pm on Jul 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There are a number of ways to proceed but the best is to obtain an instance of the database class. I prefer using a singleton factory [webmasterworld.com] as it uses a single db connection for as many classes you need to invoke to access database information.

For example, you might be using the authorization class to authenticate a user and then you might be using another class to allow them access to a certain group of methods or procedures which also require a database connection, a shopping cart perhaps. Both the authorization and shopping cart classes would invoke the db connection via the singleton factory and only a single connection to the database is made.

FnTm

4:11 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



I think I may have written my question badly.

What I would like to do, is call a function from my MySQL class from an Auth class. Like this:

$db->new Database();

class Auth
{
function something()
{
$db->insert();
{
}

Right now it tells me that i cannot redeclare a class.

mooger35

4:31 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



Here's an example that will hopefully get you pointed in the right direction:

<?php
class A {
function test() {
echo "This is TEST from class A";
}
}

class B {
function check() {
A::test();
}
}

$testing = new B;
$testing->check();
?>

idfer

2:49 am on Jul 15, 2009 (gmt 0)

10+ Year Member



Right now it tells me that i cannot redeclare a class.

Sounds a lot like you're including a php file twice. Make sure you use include_once or require_once for the files that contain purely class and function declarations.

FnTm

4:20 pm on Jul 15, 2009 (gmt 0)

10+ Year Member



Right now it tells me this:


Unknown error type: [2048] Non-static method Database::query_insert() should not be called statically, assuming $this from incompatible context
Unknown error type: [8] Undefined property: Auth::$pre

Fatal error: Call to undefined method Auth::escape() in /htdocs/serv/includes/Database.class.php on line 232

Where should I look for some good info on PHP classes? I really do need a lot of explanation :)

omoutop

2:22 pm on Jul 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



coopster gave you the answer - exactly what you are describing