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?

8
  • 2
    dependency injection - perhaps might be of interest php-di.org/doc/understanding-di.html and / or stackoverflow.com/questions/130794/what-is-dependency-injection Commented Feb 11, 2018 at 14:35
  • Does your Database class need to be instanciated? Why not make it static? Commented Feb 11, 2018 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 Commented Feb 11, 2018 at 14:41
  • 1
    Yes. static public function insert(), then in register(), use Database::insert(...). Commented Feb 11, 2018 at 14:42
  • PetarPirev, @RamRaider is right, dependency injection is the way to go. Make your classes' dependencies explicit. Commented Feb 11, 2018 at 14:43

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

Comments

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
 }
}

Comments

-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.

1 Comment

Clear way to do this is dependency injection through constructor method. otherwise you can't do unit testing and this is bad pattern

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.