안녕하세요 프로그램 작성하다 궁금한점 있어서 여쭤 봅니다.
아래에서 DB인스턴스 초기화를 2가지 방법으로 해 놓았습니다.
생성자에서 초기화, 메서드에서 초기화 .. 이렇게 두가지 방법에 대해....
생성자 초기화시에는 딸린 메서드들이 전부 안나오는듯하여 혹시 둘의 차이가 있을까 하여 여쭙니다.
답변주시는 분들 항상 감사 드립니다.
class Book_model extends Model {
protected $db;
public function __construct(){
$this->db = \Config\Database::connect();
}
public function insertTable() {
$db = \Config\Database::connect();
}
}
<?php namespace App\Models;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;
use CodeIgniter\Validation\ValidationInterface;
class UserModel extends Model
{
protected $table = 'users';
function __construct(ConnectionInterface &$db = null, ValidationInterface $validation = null)
{
parent::__construct($db, $validation);
$this->db = \Config\Database::connect();
}
}
잘 작동됩니다..
에디터에서 인식하는 방법의 차이입니다.
객체 자체는 변함 없습니다.