简体   繁体   中英

PHP how to call function from another class?

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?

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() {

   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM