1. Halo Guest, pastikan Anda selalu menaati peraturan forum sebelum mengirimkan post atau thread baru.

Mana kode PHP include yang tepat?

Discussion in 'Pemrograman Web' started by xrvel, Sep 2, 2012.

  1. xrvel

    xrvel Super Hero

    Joined:
    Oct 21, 2007
    Messages:
    2,873
    Likes Received:
    947
    Location:
    Di sini
    Mana yang lebih tepat?

    1.
    PHP:
    <?php
    include('file.php');
    ?>
    2.
    PHP:
    <?php
    define
    ('DIR'dirname(__FILE__).'/');
    include(
    DIR.'file.php');
    ?>
    Kedua2nya sama2 bekerja sesuai keinginan & tidak menghasilkan error.
    Bedanya adalah : yang nomer 1 menggunakan relative file path, yang nomer 2 menggunakan absolute file path.

    Mana yang lebih tepat, relative / absolute? :D

    Jawaban :
    Saat ini saya pilih yg no 2, karena jika pengguna script menggunakan APC (atau opcode caching lainnya) sebagai opcode caching di servernya, maka APC akan bekerja jauuuh lebih cepat jika kode include menggunakan absolute path.

    Referensi :
    Code:
    http://www.php.net/manual/en/apc.configuration.php
    Bagian apc.stat

    "Be careful changing this setting. This defaults to on, forcing APC to stat (check) the script on each request to determine if it has been modified. If it has been modified it will recompile and cache the new version. If this setting is off, APC will not check, which usually means that to force APC to recheck files, the web server will have to be restarted or the cache will have to be manually cleared. Note that FastCGI web server configurations may not clear the cache on restart. On a production server where the script files rarely change, a significant performance boost can be achieved by disabled stats.

    For included/required files this option applies as well, but note that for relative path includes (any path that doesn't start with / on Unix) APC has to check in order to uniquely identify the file. If you use absolute path includes APC can skip the stat and use that absolute path as the unique identifier for the file."

    Referensi pendukung lain :
    Code:
    http://stackoverflow.com/questions/8356547/php-absolute-vs-relative-paths
    "Absolute paths are better from a performance point of view when using an opcode cache or lots of require/include statement (although its only noticeable when you start to include hundreds of files, as might happen when using a framework like Zend/Symfony/etc).

    With a relative path the opcode cache and php must work out the files realpath each time before it can work out if it already knows about the file and if it needs to load it again. PHP internally maintains a hashmap of files to file locations which is very quick as long it doesn't have to do the above calculation each time."
     
    Last edited: Sep 2, 2012
    be4rt, tsaniy, khoid and 3 others like this.
  2. teguhaditya

    teguhaditya Super Hero

    Joined:
    Jan 23, 2008
    Messages:
    7,503
    Likes Received:
    1,418
    Location:
    _ ▂ ▃ ▅ ▆ █
    saya biasa pake nomer 2
     
  3. GrafitianZ

    GrafitianZ Super Hero

    Joined:
    Nov 6, 2010
    Messages:
    3,444
    Likes Received:
    202
    Location:
    JakCity
    ane malah sring pakai yg no 1 nih, soalnya kliatannya lebih simple :hmm:
    Nice share mastah ilmunya.... :senyum:
     
  4. masihbelajar

    masihbelajar Super Hero

    Joined:
    Jan 13, 2010
    Messages:
    1,447
    Likes Received:
    97
    slama ini pake yang no 1, yang no 2 baru ngeh
     
  5. idrea

    idrea Ads.id Fan

    Joined:
    Oct 13, 2010
    Messages:
    219
    Likes Received:
    18
    Location:
    Cibubur
    Klo masih satu folder menurut ane sebaiknya pake no 1 aja :D

    Sent using Tapatalk
     
  6. netrix

    netrix Super Hero

    Joined:
    Jan 5, 2009
    Messages:
    1,494
    Likes Received:
    242
    Location:
    Not Telling
    ane malah baru taw yang nomer 2 :(
     
  7. xrvel

    xrvel Super Hero

    Joined:
    Oct 21, 2007
    Messages:
    2,873
    Likes Received:
    947
    Location:
    Di sini
    Untuk yang mengutamakan kecepatan eksekusi kode, apalagi pake APC, nomer 2 jauhhh lebih cepet. :D
     
  8. yoenk

    yoenk Ads.id Starter

    Joined:
    Sep 24, 2009
    Messages:
    78
    Likes Received:
    6
    biasanya pake nomor 1 .....
    tapi setelah membaca ini ...mau coba2 yang nomor 2 ...:O)
     
  9. dealovies

    dealovies Hero

    Joined:
    Aug 10, 2009
    Messages:
    650
    Likes Received:
    13
    Location:
    Kuningan, Indonesia
    Baru tau ada yang nomor dua. Biasanya pakai yang nomor 1. Maklum belajar otodidak hasil ngintip2 :D
     
  10. bons87

    bons87 Super Hero

    Joined:
    May 22, 2011
    Messages:
    1,119
    Likes Received:
    62
    Location:
    Bukan Kota
    kalo filenya bnyak ane pake no. 2 soalnya biar simple klo mu ganti directory.. skrg bru tau bedanya.
     
  11. gecko

    gecko Ads.id Pro

    Joined:
    Apr 23, 2010
    Messages:
    315
    Likes Received:
    164
    Location:
    http://gecko.web.id
    Nomer 1 bukannya absolut juga. kalo g masuk dir napa perlu pake dir?
     
  12. nicefirework

    nicefirework Super Hero

    Joined:
    Aug 21, 2010
    Messages:
    1,304
    Likes Received:
    251
    gimana cara menggunakan APC, ajari donk bro.. web ane lemot banget:)
     
  13. xrvel

    xrvel Super Hero

    Joined:
    Oct 21, 2007
    Messages:
    2,873
    Likes Received:
    947
    Location:
    Di sini
    Itu masih termasuk relative :)
    CMIIW.

    Kalo g masuk dir napa perlu pake dir >>> karena AFAIK kalo pake dir dianggap absolute. Dan kalo absolute pasti lebih cepet kalo pake opcode cache :D
    It's all about speed, gan.

    APC diinstall di VPS / Dedicated server gan.
    Setelah itu masih harus pake fungsi (kode) khusus di PHP. Utk lebih cepetnya, kalo di wordpress bisa pake plugin cache. Tapi APC tetep harus diinstall di server :)
     
  14. runebal

    runebal Newbie

    Joined:
    May 31, 2011
    Messages:
    21
    Likes Received:
    0
    thanks om,, berguna banget...
     
  15. mp3online

    mp3online Super Hero

    Joined:
    Jul 19, 2011
    Messages:
    2,228
    Likes Received:
    294
    Location:
    jakarta
    kalau dibandingin ama require bagusan mana bos?
     
  16. bosspulsa

    bosspulsa WWW.WARMACHINE.IM

    Joined:
    Jan 27, 2008
    Messages:
    4,036
    Likes Received:
    608
    Location:
    Bantoel, Yogyakarta, Indonesia, Indonesia
    Buat tambahan ilmu, soalnya lagi belajar modifikasi themes gratisan, karena ga bisa membuat dari awal.
     
  17. renogen

    renogen Super Hero

    Joined:
    Jan 4, 2010
    Messages:
    1,999
    Likes Received:
    109
    Location:
    Blora
    Aku cenderung menggunaka no 1 lebih simple, tapi kalo file banyak bener juga jawaban dari mastah bons87. Kelihatan bedanya kalo ganti directory.
     
  18. xrvel

    xrvel Super Hero

    Joined:
    Oct 21, 2007
    Messages:
    2,873
    Likes Received:
    947
    Location:
    Di sini
    include > kalau ada error, kode di bawah tetep dieksekusi
    require > kalau ada error, script berhenti di baris itu.

    sebenernya bukan masalah simple dalam penulisan / nggak.
    tapi saya share info perbedaan dari sisi kecepatan eksekusi kode di server :)
     
  19. mamang-k

    mamang-k Newbie

    Joined:
    Dec 17, 2008
    Messages:
    28
    Likes Received:
    4
    Biasa pake nomor 2 gan, tapi karena sekarang dah pake CI, dah jarang pake include lagi
     
  20. icikicik

    icikicik Newbie

    Joined:
    Oct 30, 2012
    Messages:
    15
    Likes Received:
    0
    biasa pake #1 Om ... cepet dan mudah logic nya :D
    tp klo udah ngomongin performance ngikut #2 deh :D
     

Share This Page