Page 1 of 1

The guys do not know inheritance?

Posted: Thu Aug 27, 2009 3:11 pm
by kaiquej
I have seen some class written as follows:

1-

Code: Select all

 
class MyDB extends mysqli
{
     ..
    .....
    public function __construct()
    {
            .....
            [b]@mysqli_real_connect[/b](
                $this->_host,
                $this->_username,
                $this->_password,
                $this->_dbname
            );
 
    }
}
 
If you are using inheritance, why not do so?

Code: Select all

class MyDB extends mysqli
{
    public function __construct()
    {
        [b]parent[/b]::real_connect(
            $this->_host,
            $this->_username,
            $this->_password,
            $this->_dbname
        );
    }
}
 
What is the most elegant way to write? The first or second? Why?

Thanks

Re: The guys do not know inheritance?

Posted: Thu Aug 27, 2009 6:33 pm
by AlanG
I'd write it like this... (If you are using OOP, why bother with procedural methods?)

Code: Select all

 
<?php
class MyDB extends MySQLi {
    public function __construct($host,$user,$pass,$name) {
        @parent::__construct($host,$user,$pass,$name);
        
        if($this->connect_error)
            die('MySQL server connection failure.');
    }
}
?>