Citat:
Ursprungligen postat av jimmie
Citat:
Originally posted by -koala@Feb 15 2007, 19:57
Citat:
Ursprungligen postat av Seattlegrunge
Hur ofta använder man include_once() och require_once() istället för include() och require() tro? Har aldrig använt förstnämnda faktiskt.
|
Själv använder jag i princip bara require_once.
|
Mer overhead att använda *_once, ur effektivitetssynpunkt bör man använda include/require utan _once.
|
Tack för tipset. Har inte tänkt på det faktiskt. Dock är det numera (som tur är) sällan jag behöver inkludera filer, eftersom jag använder ramverket Symfony, som sköter det själv. Hittade följande på PHP.net:
Citat:
require_once (and include_once for that matters) is slow.
Furthermore, if you plan on using unit tests and mock objects (i.e. including mock classes before the real ones are included in the class you want to test), it will not work as require() loads a file and not a class.
To bypass that, and gain speed, I use :
Kod:
<?php
class_exists('myClass') || require('path/to/myClass.class.php');
?>
I tried to time 100 require_once on the same file and it took the script 0.0026 seconds to run, whereas with my method it took only 0.00054 seconds. 4 times faster ! OK, my method of testing is quite empirical and YMMV but the bonus is the ability to use mock objects in your unit tests.
|