Php-php Tutorial

Source: Internet
Author: User
Simple php class problem classdb {& nbsp; private $ host; & nbsp; private $ user; & nbsp; private $ pwd; & nbsp; private $ db; & nbsp; & nbsp; functionconnect ($ host, $ user php class simple question
Class db {
Private $ host;
Private $ user;
Private $ pwd;
Private $ db;

Function connect ($ host, $ user, $ pwd, $ db)
{
..........
}
}




Assume that I am in another file named abc. this class is introduced in php and written as follows when an object is generated: $ db = new db ("192.168.1.11", "zhangsan", "1234", "test "); how are parameters in brackets used? Is all methods in the class usable? Sorry for the answer !!!

------ Solution --------------------
Instance parameters are first passed to the class constructor, and then to the class attributes or methods.
PHP code
Class db {private $ host; private $ user; private $ pwd; private $ db; public function _ construct ($ host, $ user, $ pwd, $ db) {$ this-> connect ($ host, $ user, $ pwd, $ db);} public function connect ($ host, $ user, $ pwd, $ db) {//....}}
------ Solution --------------------
Positive solution. a constructor is required.
Discussion

Instance parameters are first passed to the class constructor, and then to the class attributes or methods.
PHP code
Class db {
Private $ host;
Private $ user;
Private $ pwd;
Private $ db;

Public function _ construct ($ host, $ user, $ pwd, $ db ){
$ This-> ......

------ Solution --------------------
Constructor.
------ Solution --------------------
You can also:
PHP code
$ Obj = new db (); $ obj-> db ("192.168.1.11", "zhangsan", "1234", "test ");
------ Solution --------------------
LS is awesome! Two at a time!
If you have the strength, you can find an internship !!

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.