Skip to main content

What is the difference between == and === operator

Firstly

 "==" 

this equality check operator try to Coercion the two value

like

 3 == '3' 

in this case == operator try convert ‘3’ in number thats why

 3 == '3'  return true;

another example

 1 < 2 < 3 

what might you think the result will be ? result is true.
On the other hand

 3 < 2 < 1 

resutl also true. You may be confuse ……..
let see what is going here…

firstly example 1

 "1 < 2 < 3" 

this

 < 

comparison operator Associativity is left-to-right so this

'<'

comparison operator Associativity is left-to-right so

 1 < 2 

execute first and result true

then

 true < 3;

here true is going to convert number and converted result is 1

then

 1 < 3 

result is true.

But on the other hand

 '===' 

Strict Equality operator dont compare in this fashoine.

It just compare the value without convert or Coercion.

Like

 3 == '3' return true;

but

 3 === '3' return false;

So main difference between

 "==" 

and

 "==="

this

 "==" 

convert the value

and this

 "===" 

dont convert the value

Code to interface not in implementation

This principle is really about dependency relationships which have to be carefully managed in a large app. – Erich Gamma

The interface represents “what” the class can do but not “how” it will do it, which is the actual implementation.

When you program to interface not implementation your code become more decouple and easy to maintenance.

For example….

 
class Mysql
{
    protected $db = null;

    public function connect($dsn, $user = '', $pass = '')
    { 
        $this->db = new PDO($dsn, $user, $pass);
    }

    public function query($query)
    {
        return $this->db->query($query);
    }
}

Here Mysql fully depend on PDO class. MySql tightly couple with PDO and it is hard to test with unit testing. Now use this class to interact with other class

 
class Book {

    private $database = null;

    public function __construct() {
        $this->database = new Mysql();
        $this->database->connect('mysql:host=localhost;dbname=oop', 'root', '');
    }

    public function getAll()
    {
       $books = $this->database->query('SELECT * FROM books ORDER BY id DESC');
       print_r($books);
    }
}

$user = new Book();
$user->getAll();

This books class also have some porblem

  • Book class fully dependent with Mysql class and it is hard to unit test.
  • If you need another model or class you have to write this mysql connection code again which is against the law DRY(dont repeat your self).
  • If you need another database or change database credential then you have to modify all class which is difficult to maintenance.
  • There is no need to know about database connection and query related work by Book class which is against (SOLID) single responsibility principle.
    So what is the solution

Solution is dependency injection or inversion which is SOLID D. Find out the dependency of the class and inject those dependency through method.

 
class Book {

    private $database = null;

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

    public function getAll()
    {
        $books = $this->database->query('SELECT * FROM books ORDER BY id DESC');
        print_r($books);
    }
}



$database = new Mysql();
$database->connect('mysql:host=localhost;dbname=oop', 'root', '');
$user = new User($database);
$user->getUsers();

After refactor the class now we can easily inject the database connection and book class does not worry about the database connection related work so it is fully single responsible. And easily test this class. But there is a problem and it is Book class only work with MySql Class.Solve this problem we can use Dependency inversion principle

 
interface DB
{
    public function connect($dsn, $user = '', $pass = '');
    public function query($query);
}

class Mysql implement DB	
{
    protected $db = null;

    public function connect($dsn, $user = '', $pass = '')
    { 
        $this->db = new PDO($dsn, $user, $pass);
    }

    public function query($query)
    {
        return $this->db->query($query);
    }
}

we can also write others database connection class which also implement DB interface.

 

class Sqlite implements DB
{
    protected $db = null;

    public function connect($dsn, $user = '', $pass = '')
    {
        $this->db = new PDO($dsn);
    }

    public function query($query)
    {
        return $this->db->query($query);
    }
}

Here come our main focus that is code to interface not to implementation

 


class Book
{

    private $database = null;

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

    public function getAll()
    {
        $books = $this->database->query('SELECT * FROM books ORDER BY id DESC');
        print_r($books);
    }
}


$database = new Mysql();
$database->connect('mysql:host=localhost;dbname=oop', 'root', '');
$book = new Book($database);
$book->getAll();

Now we can easily inject any database connection and query related class which implement DB interface.