CI 묻고 답하기

제목 무슨 에러인지 잘 모르겠습니다.
카테고리 CI 2, 3
글쓴이 호로룰루 작성시각 2018/10/05 18:33:24
댓글 : 5 추천 : 0 스크랩 : 0 조회수 : 16632   RSS

[code]

test_helper.php

$CI =& get_instance();

$CI->load->library(array('test_user'));

$agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.7';

$userAgent = new Test_user($agent);

 

 

 

Test_user.php (library)

class Test_user{

 function __construct($test) {

 }

}

[/code]

 

Message: Too few arguments to function Test_user::__construct(), 0 passed in C:\project\www\system\core\Loader.php on line 1281 and exactly 1 expected

이게 왜 에러인지 모르겠습니다..

헬퍼에서 library 로드하고 new Test_user에 값을 넘겨주면 

__construct ($test) {

 여기서 $test를 받을수있는게 아닌가요?

}

 

 

 

 다음글 컨트롤러단에서 $data['list][$key] 로 D... (3)
 이전글 pagination 질문입니다. (3)

댓글

변종원(웅파) / 2018/10/05 18:57:45 / 추천 0

헬퍼에서는 $this 객체를 사용할 수 없습니다. 제어권(?) 바깥이라 인스턴스를 선언해서 사용해야 합니다.

매뉴얼 참고하세요. http://www.ciboard.co.kr/user_guide/kr/general/creating_libraries.html#utilizing-codeigniter-resources-within-your-library

호로룰루 / 2018/10/05 19:12:09 / 추천 0
$this를 $CI 로 바꿔봐도 똑같네요 ㅠㅠ
호로룰루 / 2018/10/05 21:55:51 / 추천 0
이거 에러 아시는분 없나요.. 아침부터 지금 밤 10시까지 잡고있는데 도저히 모르겠네요 ㅋㅋㅋㅋ
변종원(웅파) / 2018/10/05 23:55:23 / 추천 0

$agent = 'Mozilla'; 이렇게 넘겨보세요. 내용에 콤마도 있고 여러개의 파라미터로 판단될 수 있습니다.

Message: Too few arguments to function 으로 구글에서 검색만 해도 답은 나옵니다. 항상 에러메세지에 답이 있습니다.

호로룰루 / 2018/10/06 00:15:46 / 추천 0

늦은밤 답변 감사합니다.

계속해서 보고있는데 조금의 답은 찾은것 같습니다.

 

헬퍼에서 $CI->load->library(array('test_user')); 라이브러리를 로드하는 즉시 Test_user.php 라이브러리 페이지를 한번 읽는군요.

이때  인자값을 주지않으니 __construct ($test) 생성자에서 에러가난것같습니다.

__construct ($test = null) 로 변경해주니 잘됩니다. 

 

class Test_user{

 function __construct($test = null) {

  echo 1; // 여기서 에코를 찍어보니 11이 찍힙니다.

  페이지를 한번읽은게아니라

  헬퍼에서 $CI->load->library(array('test_user'));  1번읽고

  $userAgent = new Test_user($agent); 1번읽고 

  총 2번을읽네요.  결론은 라이브러리 로드할때 인자값이 없어서 에러입니다.

 }

}

 

$CI->load->library(array('test_user')); 을 

include로 변경하니 또한 잘되네요. 답은 라이브러리 로드하면 로드즉시 한번읽네요