Function parameter NULL is a code for data connection initialization. I learned C # before, that is, $ dbo = NULL in the constructor. please advise.
Can I avoid being NULL? $ dbo is directly used. in my personal understanding, $ dbo = NULL indicates that $ dbo is not of the object type. Why does it need to be determined? $ Dbo = NULL is the NULL value not included in the function?
Protected function _ construct ($ dbo)
{
//...
}
class DB_Connect{protected $db;protected function __construct($dbo=NULL){if(is_object($dbo)){$this->db=$dbo;}else{$dsn="mysql:host=".DB_HOST."; dbname=".DB_NAME;try {$this->db=new PDO($dsn,DB_USER,DB_PASS);}catch(Exception $e){die($e->getMessage());}}}}
Reply to discussion (solution)
Function _ construct ($ dbo = NULL)
It indicates that $ dbo is a default parameter because it has an initial value of NULL.
If only
Function _ construct ($ dbo)
The $ dbo parameter must be passed in.
Because it is a default parameter
New DB_Connect ();
New DB_Connect ($ db );
No errors
C # supports heavy load, so you may write
DB_Connect ::__ construct ($ dbo ){}
DB_Connect ::__ construct (){}
In this case, I will understand. It is very different from C #. in C #, this parameter must be passed in. it is not in the mind... So it cannot be understood at all.
However, for object-oriented programming, overloading is a very important concept (method)