2010-06-09 6 views
4

function.php라는 외부 파일에 내 웹 사이트에 init이라는 함수가 있습니다. index.php를로드 페이지와 통화 기능 초기화 :PHP 모든 함수 변수 전역

function init(){ 
    error_reporting(0); 
    $time_start = microtime(true); 
    $con = mysql_connect("localhost","user123","password123"); 
    mysql_select_db("db123"); 
} 

어떻게 어쩌면 당신은에서 이러한 변수를 반환 할 수 있습니다

global $time_start; 
global $con; 

답변

1

를 사용하지 않고 (가능한 경우) 글로벌이 모든 변수를 얻을 수 있습니다 init() 함수 :

function init(){ 
    error_reporting(0); 
    $time_start = microtime(true); 
    $con = mysql_connect("localhost","user123","password123"); 
    mysql_select_db("db123"); 

    return array('time_start' => $time_start, 'con' => $con); 
} 
+0

고마워요! 아무도 더 나은 대답을 게시 할 수 없다면 동의하십시오. – nebkat

0

전역 범위에서 선언 한 다음 참조로 함수에 전달할 수 있습니다. 기능을 수정 한 후.

3

글로벌하지 않으려면

다른 대안은 개체에 캡슐화하거나 구성하기 위해 DI를 사용하는 것입니다.