0

I have the following problem - lets say I have a class named Database

class Database
{
   public function insert(...)
   {
    //insert something in the database
   }
}

And I have a class named User

class User
{
   public function register(..)
   {
       //validate the user , and insert him
       //here i need to call insert() function from Database class
   }
}

Is there a simple way in which I can call the insert method inside the User class?

TGrif
  • 5,725
  • 9
  • 31
  • 52
  • 2
    `dependency injection` - perhaps might be of interest http://php-di.org/doc/understanding-di.html and / or https://stackoverflow.com/questions/130794/what-is-dependency-injection – Professor Abronsius Feb 11 '18 at 14:35
  • Does your `Database` class need to be instanciated? Why not make it static? – Syscall Feb 11 '18 at 14:37
  • Syscall , no i can make it static , then just include it and call Database::insert(...) ? Is this correct , i`m new to OOP – Ivan Ivanov Feb 11 '18 at 14:41
  • 1
    Yes. `static public function insert()`, then in `register()`, use `Database::insert(...)`. – Syscall Feb 11 '18 at 14:42
  • PetarPirev, @RamRaider is right, dependency injection is the way to go. **Make your classes' dependencies explicit**. – ishegg Feb 11 '18 at 14:43
  • Syscall , thanks , how to mark your comment as a correct answer ? – Ivan Ivanov Feb 11 '18 at 14:43
  • ishegg , i heard about that , but it`s like complex to me , with these controllers and such – Ivan Ivanov Feb 11 '18 at 14:45
  • Petar, it certainly has a learning curve, but trust me, you will be thanking yourself in the long run. It'll be much more maintainable and readable. – ishegg Feb 11 '18 at 14:47

3 Answers3

2

using dependency injection you would typically do it something like this

class Database
{
   public function insert(...)
   {
    //insert something in the database
   }
}


class User
{
   private $db;
   public function __construct($db){
        $this->db=$db
   }
   public function register(..)
   {
       //validate the user , and insert him
       //here i need to call insert() function from Database class

       $this->db->insert();
   }
}

$db=new Database();
$usr=new User( $db );
$usr->register();
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0

Just make an object in the user class and call the method in it like

class User
{
public function register(..)
 {
   //validate the user , and insert him
   //here i need to call insert() function from Database class
 $useit= new Database; //assign an object of type database
  $useit->insert();  //call the method like this
 }
}
Dheeraj Joshi
  • 1,522
  • 14
  • 23
-1

Create an instance of the class you want to import in the constructor of the class you're importing it to.

class User {
    private $database;

    public function __construct() {
        $this->database = new Database;
    }

    public function register() {
        $this->database->insert();
    }
}

class Database {
    public function insert() {
    }
}

$user = new User;
$user->register();

This answer should help. You can access all methods and properties of the Database class by referencing $this->database.

Mav
  • 1,087
  • 1
  • 15
  • 37
  • Clear way to do this is dependency injection through constructor method. otherwise you can't do unit testing and this is bad pattern – Alex Jun 22 '23 at 08:43