저는 PHP 5.6.16에서 웹 페이지를 디자인하고 있으며 몇 가지 OOP 아이디어를 사용하려고합니다. index.php 파일에 classnews.php 파일을 포함하면 오류가 발생합니다.
Undefined variable: connect in classtin.php
classdb.php :
<?php
class db
{
public $hostname = "localhost";
public $user= "root";
public $pass = "";
public $db = "test";
/* public $connect = NULL;
public $result = NULL;*/
function __construct()
{
$connect = mysqli_connect($this->hostname, $this->user, $this->pass,$this->db);
mysqli_set_charset($connect,'utf8');
}
}
?>
classnews.php :
<?php
include_once('classdb.php');
class tin extends db
{
public function tinmoi()
{
$sql= "select * from emp ";
$result= mysqli_query($connect,$sql);
return $result;
}
}
?>
으로 클래스 멤버를 참조해야합니다 $this
. $this->hostname
예를 들어에서 했던 것과 같은 방식 입니다.
가지고있는 코드의 일부 수정 :
class db
{
public $hostname = "localhost";
public $user= "root";
public $pass = "";
public $db = "test";
public $connection;
public function __construct()
{
$this->connection = mysqli_connect($this->hostname, $this->user, $this->pass,$this->db);
mysqli_set_charset($this->connection,'utf8');
}
}
class tin extends db
{
public function tinmoi()
{
$sql = "select * from emp ";
return mysqli_query($this->connection, $sql);
}
}
다시 말하지만, PHP에서 OOP의 기본 사항을 살펴볼 것을 강력히 제안합니다 : http://php.net/manual/en/language.oop5.basic.php
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다