2012-07-05 4 views
0

가능한 중복 : 나는 PHP 프레임 워크를하고 있어요
autoload functions in php누락 된 기능이 자동으로 포함됩니까?

. 함수가 존재하지 않을 때 오류 처리기를 다시 작성하여 자동으로 함수를 설명하는 파일을 포함하려고 시도하는 방법이 있는지 궁금합니다.

예 :

echo general_foo(); // <-- general_foo() is not yet stated. 
        // A handler tries to include_once('functions/general.php') based on the first word of the function name. 
        // If the function still doesn't exist - throw an error. 

이에서 승리 불필요한 파일을 컴파일 건너 뛰거나 트랙 상태를 유지하는 것은 여기 저기에 포함 건너 뛸 것입니다.

클래스가 아닌 함수에 대해서만 __autoload.

+0

개인/내부 프레임 워크 인 경우 f ('fn', array ([args]))와 같은 것을 제리 리그 할 수 있습니다. 그러나 나는 그 트레이드 오프가 가치 있다고 생각하지 않는다. –

+0

가능한 중복의 [자동로드 기능을 PHP에서] (http://stackoverflow.com/questions/4196881/autoload-functions-in-php) - err - [Autoloader for functions (19 Jan 2011)] (http : // stackoverflow .com/questions/4737199/autoloader-for-functions) – hakre

답변

1

존재하지 않으며 절대로 그렇게하지 않을 것입니다. 그래, 나는 그것을 원한다. 그러나 이것은 정적 함수를 가진 클래스를 사용하는 것을 막지 않으며 PHP가 자동로드되도록한다.

class functions { 

    public function __call($function, $arguments) { 

     if (!function_exists($function)) { 
     $function_file = 'path/to/functions/' . substr($function, 0, strpos($function, '_')).'.php'; 
     include_once($function_file); 
     } 

     return call_user_func_array($function, $arguments); 
    } 
    } 

함수 파일 기능/test.php

function test_foo() { 
    return 'bar'; 
    } 

:

http://php.net/spl-autoload-register

+0

함수가 아닌 메서드를 호출하면 클래스 이벤트 내부에서 비슷한 것을 얻을 수 있습니까? – tim

+0

예. 대답에 대한 링크를 추가했습니다. 하지만 먼저 "PSR-0"과 "PSR-0 Classloader"에 대해 google을 사용해야합니다 (그리고 이미 "Composer"라고 검색했기 때문에). 그래서 바퀴를 재발 명할 필요가 없습니다. – KingCrunch

+0

__call()을 사용하여 해결되었습니다. 게시 된 솔루션을 참조하십시오. – tim

-1

나는이

같은 클래스 파일 클래스/functions.php 그것을 해결 스크립트 myscript.php :

require_once('classes/functions.php'); 
    $functions = new functions(); 

    echo $functions->test_foo(); // Checks if function test_foo() exists, 
           // includes the function file if not included, 
           // and returns bar 

결국 __autoload()를 사용하여 classes/functions.php를 자동로드 할 수 있습니다.

결국 my_function()의 구문은 $ functions-> my_function()이됩니다. 함수가 존재하지 않으면 자신 만의 오류 처리기를 작성할 수 있습니다. ;)

관련 문제