Home > Backend Development > PHP Tutorial > php类的简单有关问题

php类的简单有关问题

WBOY
Release: 2016-06-13 10:29:13
Original
817 people have browsed it

php类的简单问题
class db{
  private $host;
  private $user;
  private $pwd;
  private $db;
   
  function connect($host,$user,$pwd,$db)
  {
  ..........
  }
}




假如说我在另一个文件假设名为abc.php里引入这个类,在生成对象的时候这样写:$db=new db("192.168.1.11","zhangsan","1234","test");括号里的参数是怎么个用法?是类里所有方法都可以用吗?跪求详细解答!!!

------解决方案--------------------
实例参数会首先传递给类的构造方法,构造方法再传递给类的属性或者方法

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){        //....    }}<br><font color="#e78608">------解决方案--------------------</font><br>正解,要 有个构造函数<br>
Copy after login
探讨

实例参数会首先传递给类的构造方法,构造方法再传递给类的属性或者方法
PHP code
class db{
private $host;
private $user;
private $pwd;
private $db;

public function __construct($host,$user,$pwd,$db){
$this->……

------解决方案--------------------
构造函数。
------解决方案--------------------
也可以:
PHP code
$obj = new db();$obj->db("192.168.1.11","zhangsan","1234","test");<br><font color="#e78608">------解决方案--------------------</font><br>LS的给力啊!一次来2个!<br>有实力的话实习机会很好找的!!<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template