0

I basically have 3 important files right now. I want to use a function from the DB class in the LOGIN class. These are my files. I tried including the DB class but then I would be declaring it twice, which you cant do.

---- index.php ---- -- Which displays the content --

<?php session_start(); include './libs/database.php'; $mysql = new Database(); include './libs/login.php'; $login = new Login(); $mysql->connect("---", "user", "pass"); $mysql->usedatabase("db"); ?> <!DOCTYPE html> <html> <body> <div id="wrapper"> CONTENT GOES HERE </div> </body> </html> --- ---login.php --- class Login{ public function isLoggedIn(){ if(isset($_SESSION['user_id'])){ return true; }else{ return false; } } public function UserLogin($email,$password){ // login function $DB->selectwhere(...); } public function securePassword($pass){ $pass = md5($pass); return $pass; } } --- --- database.php --- class Database{ //Database Functions } 
2
  • 6
    could you not just use require_once or include_once uk.php.net/include_once Or look at how you structure your classes and pass a reference to your DB class or use a static class Commented Aug 29, 2012 at 17:25
  • I don't know why people use include anyway. It's not like execution should continue even without your core files. Use require_once or autoloading. But remember that the _once part adds a little overhead, and also it's a mess, so you better learn about autoloading and PSR-0 if possible. Commented Aug 30, 2012 at 19:39

4 Answers 4

2

Look into PHP's "Autoload" capabilities. You can actually look up classes on-demand ONLY when they don't already exist in the heap.

Sign up to request clarification or add additional context in comments.

1 Comment

With this, I'm getting an error that says "$mysql is Undefined", and "Call to a member function selectwhere() on a non-object"
1

Make the securePassword() function in Login a static function so you can call it without an instance of the Login class.

Login::securePassword()

Comments

0

Move your Database class to file Database.php. At the top of index.php, use require_once('Database.php'); to include the Database class. Then change UserLogin() function to this:

 public function UserLogin($email,$password){ // login function $DB = new Database; $DB->selectwhere(...); } 

Comments

0

Read the PHP.net documentation about the keyword extends: http://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.extends

A class can inherit the methods and properties of another class by using the keyword >extends in the class declaration.

So your login class will be

class Login extends Database{ public function isLoggedIn(){ if(isset($_SESSION['user_id'])) { return true; } else { return false; } } public function UserLogin($email,$password){ parent::login(); } public function securePassword($pass){ $pass = md5($pass); return $pass; } } 

You can access a property or method of the Database class using

parent::var_name parent::method() 

1 Comment

This is the way to go, however I would also look at combining this solution with the answer of Stephen305. When extending a class, it is always good practice to make sure that the class is indeed already loaded, otherwise you will run into trouble. This is of course presuming the classes will be in seperate files.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.