-1

I wonder, why is return used for in this code? What happend after say to return the variable? And what is the difference if I return and don't return?

<?php



class Database{
    private $conn;

    public function Connect($host, $dbname, $user, $pass)
    {
        
        $this->conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
        return $this->conn;

    }
}
  • 2
    An obvious answer would be "so that the user of that class can store the connexion in a variable", but that would defeat the fact that it is set to `private`. We can't really answer you without knowing how this class is supposed to be used, what kind of other methods it offers and such. At first glance it looks like a bad design, but if there are no other methods in the class, the class couldn't be used without this method returning the connexion, as it would not be available.. – Kaddath Jul 21 '20 at 12:20
  • I just created this as an example, this will be a class to connect to the database, what I wonder is if I need to use return or no, I don't know what will return do, can you understand? – Paula Silva Jul 21 '20 at 12:23
  • The return will expose `$this->conn` as the result of the call to `Connect` method. Now it really depends on what you planned this class will be, will it be used for the connexion actions only (in that case the connexion should be returned at one step or another so you can perform actions on the database), OR is the class meant to handle all the actions on the database: inserts, queries, etc (in that case it should not be returned and only used internally)? – Kaddath Jul 21 '20 at 12:38
  • It almost looks like a singleton pattern [PHP singleton database connection pattern](https://stackoverflow.com/questions/21832809/php-singleton-database-connection-pattern). – Nigel Ren Jul 21 '20 at 12:44
  • Use return if you need it. We can't tell you whether you need it or not. It depends whether you want code which calls this Connect function to be able to get a copy of the $conn variable. If you want to allow that, then keep the return statement. If you don't want that, then don't. There's no right or wrong answer here that we can provide, it simply depends what you actually want the behaviour of the code to be (which you didn't make clear). – ADyson Jul 21 '20 at 13:39

2 Answers2

0

Return is used in code to give a value to a variable when you execute a method. So using your example:

// 1. Example without return in your code
// Here you can't use the database connection
$database = new Database();
$database->connect('host', 'dbname' 'user', 'password');

// you would need to implement this:
$connection = $database->getConnection(); 
// to use this:
$connection->prepare('DELETE name FROM people');

// 2. Example with return in your code
// Here you make use of the database connection
$database = new Database();
$connection = $database->connect('host', 'dbname' 'user', 'password');
$connection->prepare('DELETE name FROM people');
0

what is the difference if I return and don't return?

If you capture the returned value but don't return it, the difference is that you'll get null instead of \PDO:

class Database
{
    private $conn;

    public function Connect($host, $dbname, $user, $pass)
    {
        $this->conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    }
}
$database = new Database();
$connection = $database->Connect('localhost', 'test', 'test', 'test');
var_dump($connection); // NULL

If you don't capture it, there's no difference (aside from the potential of being able to catch the value in the future).

Additionally, since objects are passed by reference (rather than value) capturing the value has two side effects:

  • You have direct uncontrolled access to a private property:

    $database = new Database();
    $connection = $database->Connect('localhost', 'test', 'test', 'test');
    $connection->rollBack(); // Why not?
    
  • You might be able to use the method to obtain two different connections from the same instance (something the class is not designed to handle):

    $database = new Database();
    $connection1 = $database->Connect('localhost', 'test', 'test', 'test');
    $connection2 = $database->Connect('localhost', 'test', 'test', 'test');
    var_dump($connection1, $connection2);
    
    object(PDO)#2 (0) {
    }
    object(PDO)#3 (0) {
    }
    

In short, it's a pretty pointless confusing head-scratching design. I don't know what features you have in mind with this class but this doesn't seem to be the way.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360