load/require specific php files for specific pages/templates/post types

I think you have a bit of mixing up in different purposes to file includes, which are normally separated in modern PHP development: definitions and runtime code.

Definitions

Files that include definitions (such as classes and functions) should not contain runtime (immediately executing) code. That makes safe them to include early (quite optimized operation in proper PHP stack, more so with opcode caching) or lazily (class autoload).

Runtime code bootstrap

In WordPress context it is normal practice to stagger runtime code in following way:

  1. As soon as your extension loads you hook logic to init hook and that’s all (with some variations for more specific needs).
  2. When init hook fires, your logic further hooks necessary actions to appropriate hooks.

Overall

Includes are just a poor mechanism to implement timing and logic with.

Their role should be limited to load process and further actions should be handled with appropriate event system (hooks in WP case).

Leave a Comment