PHP 4와 5 둘다 호환되게끔 코딩하는 법에 대한 안내서가 없을까요?

vudghkzm의 이미지

PHP4 와 PHP5는 제법 많은 것이 다르다고 알고 있는데요. 특히 OOP에서 말이죠.

PHP4와 PHP5 두군데에서 둘다 정상적으로 동작하게끔 OOP 코드를 짜는 방법에 대한 안내를 해 놓은 문서 혹은 책자가 있는지 궁금하네요.

PHP 매뉴얼에는 PHP4에서 PHP5로 마이그레이션하는 방법만 소개해놓고, 두 버전에 둘다 호환되게끔 코드를 짜는 방법에 대해서는 소개가 없네요.

아니면 정말 PHP4와 PHP5 둘다 호환되게끔 OOP 코드를 짜기는 힘든걸까요?

카二리의 이미지

그냥 4를 기준으로 짜시면 5에서도 하위호환이 가능 합니다.

새 생각 :)

strongberry의 이미지

vudghkzm wrote:
PHP4 와 PHP5는 제법 많은 것이 다르다고 알고 있는데요. 특히 OOP에서 말이죠.

PHP4와 PHP5 두군데에서 둘다 정상적으로 동작하게끔 OOP 코드를 짜는 방법에 대한 안내를 해 놓은 문서 혹은 책자가 있는지 궁금하네요.

PHP 매뉴얼에는 PHP4에서 PHP5로 마이그레이션하는 방법만 소개해놓고, 두 버전에 둘다 호환되게끔 코드를 짜는 방법에 대해서는 소개가 없네요.

아니면 정말 PHP4와 PHP5 둘다 호환되게끔 OOP 코드를 짜기는 힘든걸까요?

제 기억으로 $PHP_VERSION 상수를 통해 버전을 확인하고 각 버전에 따른 변경점(특히 함수)를 정의해서 사용하면 간편할듯 합니다. 변경점을 모두 패치하는것(아마 누군가는 했을지도 :) )이 관건이겠지만요

============================================
자나깨나 트롤 조심. 나간 트롤 다시보자.
"저는 앞으로 troll을 만나더라도 먹이를 주지 않도록 노력하겠습니다." :)

sh.의 이미지

version_compare()이나 phpversion() 함수를 통해서 현재의 버전을 검사하거나, 특정 버전하고 비교를 할 수도 있기는 합니다만 이런것들로 php5와 4의 차이점을 극복하기는 어렵단 생각이 듭니다. 특히 oop와 관련해서는 카이리님 말씀처럼 4를 기준으로 작성하시는 방법밖에 없다고 봅니다.

아래는 pear에 있는 DataObject.php의 일부분인데요 혹시 도움이 되실까 해서 올려봅니다.

// this will be horrifically slow!!!!                                                                                                                                  
// NOTE: Overload SEGFAULTS ON PHP4 + Zend Optimizer (see define before..)                                                                                             
// these two are BC/FC handlers for call in PHP4/5                                                                                                                     
                                                                                                                                                                       
if ( substr(phpversion(),0,1) == 5) {                                                                                                                                  
    class DB_DataObject_Overload                                                                                                                                       
    {                                                                                                                                                                  
        function __call($method,$args)                                                                                                                                 
        {                                                                                                                                                              
            $return = null;                                                                                                                                            
            $this->_call($method,$args,$return);                                                                                                                       
            return $return;                                                                                                                                            
        }                                                                                                                                                              
        function __sleep()                                                                                                                                             
        {                                                                                                                                                              
            return array_keys(get_object_vars($this)) ;                                                                                                                
        }                                                                                                                                                              
    }                                                                                                                                                                  
} else {                                                                                                                                                               
    if (version_compare(phpversion(),'4.3.10','eq') && !defined('DB_DATAOBJECT_NO_OVERLOAD')) {                                                                        
        trigger_error(                                                                                                                                                 
            "overload does not work with PHP4.3.10, either upgrade                                                                                                     
            (snaps.php.net) or more recent version                                                                                                                     
            or define DB_DATAOBJECT_NO_OVERLOAD as per the manual.                                                                                                     
            ",E_USER_ERROR);                                                                                                                                           
    }                                                                                                                                                                  
                                                                                                                                                                       
    if (!function_exists('clone')) {                                                                                                                                   
        // emulate clone  - as per php_compact, slow but really the correct behaviour..                                                                                
        eval('function clone($t) { $r = $t; if (method_exists($r,"__clone")) { $r->__clone(); } return $r; }');                                                        
    }                                                                                                                                                                  
    eval('                                                                                                                                                             
        class DB_DataObject_Overload {                                                                                                                                 
            function __call($method,$args,&$return) {                                                                                                                  
                return $this->_call($method,$args,$return);                                                                                                            
            }                                                                                                                                                          
        }                                                                                                                                                              
    ');                                                                                                                                                                
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.