Function parameter NULL

Source: Internet
Author: User
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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.