0

I have a class Database in database.php with 145 functions(17,000 lines) and now i've read this is bad practice, so i want sort out the functions correctly into specific classes rather than a "God" Class.

What i want to know is how do i call a function from another class? Below is an example; How do i call function two from within function one?

database.php

require("connect.php");

class Database {
private $connect;

function one() {
//call function two
}

}

forms.php

require("connect.php");

class Forms {
private $connect;

function two() {
//returns forms
}

}

How do i do this?

5
  • 2
    How do you usually call functions of a class? You create an object and then? :) Commented Feb 13, 2014 at 14:48
  • Do i include 'forms.php'; at the top of database.php? Commented Feb 13, 2014 at 14:51
  • You'd have a problem if you did that. Both files require connect.php User require_once instead Commented Feb 13, 2014 at 14:53
  • So should i remove require connect.php from forms class? In forms class i also connect to the database. Commented Feb 13, 2014 at 14:53
  • Change require to require_once Commented Feb 13, 2014 at 14:55

1 Answer 1

1

In the example you gave you would do:

function one() {
    $forms = new Forms;
    $forms->two();
}

Another option would be

function one() {
      Forms::two();
   }

And in Forms you would change the method to:

static function two() {

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.