Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我不能在PHP构造函数中使用现有类吗?_Php_Methods_Constructor_Mysqli - Fatal编程技术网

我不能在PHP构造函数中使用现有类吗?

我不能在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->

我对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->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 );
    }
}