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 !!