function parameter null problem

Source: Internet
Author: User
Tags dsn
A data connection initialization code, I previously learned C #, is the constructor of the $dbo=null this can not understand, please advise.
I can not =null, directly with $dbo, I personally understand is $dbo=null this statement means that $dbo is not an object type, then why do you want to judge? $dbo =null This null value does not go inside the function?
protected function __construct ($dbo)
{
//...
}
Class db_connect{protected $db;p rotected 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)
Indicates $dbo This parameter is default, because he has an initial value of NULL
If it is only
function __construct ($dbo)
Then $dbo this parameter must be passed in

Due to the default parameters,
New Db_connect ();
New Db_connect ($DB);
It's not going wrong.


C # supports overloading, so for this case you might be writing
Db_connect::__construct ($dbo) {}
Db_connect::__construct () {}

That's what I understand. and C # very different, Ah, C # in this parameter must be passed in, the mind is not the concept of ... So I can't understand it at all.

But object-oriented programming, overloading is a very important concept (method)

  • 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.