-1
방금 PHP 클래스를 만들었는데 클래스에 대한 내 머리와 방법에 대해 계속 생각하고 있습니다. 어떻게 "성공적으로 연결되었습니다!" echo'ed 두 번 도착? 나는 그것을 조사해 보았고 왜 두 번 인쇄 할 것인지에 대해 내 머리를 터지게하지 못했습니다.PHP 데이터베이스 클래스, 두 번 에코
<?php
include 'dbConfig.php';
class dbConnect extends dbConfig
{
public $connectionString;
public $dataSet;
private $sqlQuery;
protected $host;
protected $userName;
protected $passWord;
protected $dbName;
function dbParams(){
$this->connectionString = NULL;
$this->sqlQuery = NULL;
$this->dataSet = NULL;
// Creates an object that is created on the extend (dbConfig)
$dbParams = new dbConfig();
//Sets the variables in this class to match dbConfig's
$this->host = $dbParams->host;
$this->userName = $dbParams->userName;
$this->passWord = $dbParams->passWord;
$this->dbName = $dbParams->dbName;
// Object no longer needed, Null the object
$dbParams = NULL;
}
function dbConnect(){
$this ->connectionString = mysqli_connect($this->host, $this->userName, $this->passWord, $this->dbName);
if($this->connectionString->connect_error){
echo "Connection failed";
} else{
echo "Successfully Connected!";
}
return $this->connectionString;
}
}
아아, 그래서 때 당신은 자동으로 당신에게 하나를 assing하는 __construct를 정의하지? – NathanK
@ NathanK 이것은 PHP4 생성자 방법론입니다. 함수의 이름을 클래스 이름과 같게 지정하면 생성자가됩니다. PHP 5에 보관되었으며 현재 PHP 7에서 사용되지 않습니다 (http://php.net/manual/en/migration70.deprecated.php). – Machavity
실제로 생성자가 필요하면'__construct()'만 사용하는 것이 좋습니다. __construct()가 없으면 단순히 실행되지 않습니다. 그러나 당신은'dbConnect()'메쏘드를 가짐으로써 암시적인 생성자를가집니다. 그래서 실행됩니다. __construct()가 있으면 classname() 버전이 무시됩니다. –