Hi,

Does require_once("myclass.class"); adds overhead to php page if it is not necessarly needed for current page?

I ask this because i was thinking to add all classes in header.php and call header.php in every other pages even some classes are not needed for some pages.

Thanks

Dani AI

Generated

As is thinking about a header that pulls in every class, and as noted, there is some cost to pulling in files you do not need. The important nuance is how much that cost matters in your environment. Each "once" include requires PHP to check whether the file was already loaded and may trigger filesystem access and parsing. With many small files or on hosts without an opcode cache, that can add measurable I/O and CPU work. See the PHP docs on include/require_once for details (require_once).

If the server runs an opcode cache (OPcache), the runtime cost of repeated includes is much lower because compiled code is kept in memory; enabling OPcache is often the single best win for include-heavy apps (OPcache). Long term, prefer on-demand loading for classes rather than an all-in-one header: use SPL autoloading or a PSR-4/Composer autoloader so class files are only loaded when needed (spl_autoload_register, PSR-4).

Practical steps: enable OPcache if possible, move only truly global/essential includes into a bootstrap, adopt autoloading for class files, and measure before optimizing (profilers or simple timing). This keeps code clean and avoids unnecessary per-request work without guesswork.

Recommended Answers

All 2 Replies

I'm not sure what you mean by overhead but if you are meaning will it take more cpu and longer to load then yes is the answer. And is that a java class because they are meant to be called via html code and not php unless you have worked out how to make php communicate with java.

Yes i meant to say cpu power and memory usage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.