我不能在PHP构造函数中使用现有类吗?
我对OOP有点陌生,尝试使用类转换许多现有代码。我的第一个类是Job,我的主脚本调用:我不能在PHP构造函数中使用现有类吗?,php,methods,constructor,mysqli,Php,Methods,Constructor,Mysqli,我对OOP有点陌生,尝试使用类转换许多现有代码。我的第一个类是Job,我的主脚本调用: ## ## 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->
##
## 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']);
在我的job.class.php中,我试图使用构造函数打开与数据库的连接并为数据库建立链接,$\u dblink被定义为private:
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方法时,我得到了一个致命的错误:调用非对象上的成员函数查询
我不明白为什么我的私有$\u dblink变量没有传递到后续方法。我缺少什么?您必须使链接成为类成员,以便以后可以使用$this->操作符 使用$this->\u dblink访问$\u dblink。
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;
}
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 );
}
}