2012-10-30 4 views
0

저는 OOP에 다소 익숙하며 클래스를 사용하여 기존 코드를 많이 변환하려고합니다. 나의 첫 번째 클래스는 "작업"이며, 내 주요 스크립트 호출 내 job.class.php 내부 PHP 생성자에서 기존 클래스를 사용할 수 없습니까?

## 
## New JOB Instance begins 
## 
$job = new Job(); 
$job->validate($job_status,$ber_order_number); 
$job->sID($job->ExistingOrNew()); 
$job->parseJobInfo($msg_blocks['Job Information']); 
$job->parseLocationInfo($msg_blocks['Location Address']); 
$job->parseContactInfo($msg_blocks['Contact Information']); 

, 나는에 대한 연결을 열고 내 데이터베이스에 대한 링크를 설정하기 위해 생성자를 사용하기 위해 노력하고있어, $의 _dblink가 비공개로 정의된다

function __construct() { 
    $_dblink = new mysqli($this->_server, $this->_user, $this->_pass, "jobs");  // connection to the jobs database 
    if ($_dblink->connect_error) { 
     die('Connect Error (' . $_dblink->connect_errno . ') ' . $_dblink->connect_error); 
    } 
} 

를 최대한 빨리 내 "ExistingOrNew"방법을 히트로, 나는 치명적인 오류가 발생합니다 : 비 객체() 멤버 함수 쿼리에 전화

function ExistingOrNew() { 
    $q = "SELECT id FROM " . jJOBS . " WHERE order_number = '" . $this->order_number . "'"; 
    # 
    echo "<pre>"; 
    print_r($this); 
    echo "</pre>"; 
    # 
    if ($r = $this->_dblink->query($q)) { 
     while ($row = $r->fetch_assoc()) { 
      $id = $row['id']; 
     } 
     $r->free(); 
    } 
    if (empty ($id)) { 
     $id = $this->Create(); 
    } 
    return $id; 
} 

내 개인 $ _dblink 변수가 후속 방법으로 전달되지 않는 이유를 모르겠습니다. 내가 뭘 놓치고 있니?

+0

'$ this -> _ dblink' **를 사용하여'$ _dblink'에 액세스하십시오. – Florent

답변

2

링크를 클래스 멤버로 설정해야 나중에 $this-> 연산자와 함께 사용할 수 있습니다.

function __construct() { 
    $this=>_dblink = new mysqli($this->_server, $this->_user, $this->_pass, "jobs");  // connection to the jobs database 
    if ($this->_dblink->connect_error) { 
     die('Connect Error (' . $_dblink->connect_errno . ') ' . $_dblink->connect_error); 
    } 
} 
+0

가 - 빠른 답변에 감사드립니다. 나는 마약처럼 느껴져! ;) – DevlshOne

관련 문제