I think of Drupal as a library of PHP functions useful for developing a dynamic website. It's much more than that, and non-programmers do get it to do great things, but thinking about as a programming library is the way to get the most power out of the software.
As a library, Drupal is object oriented (although it does not use PHP classes to accomplish this), and based on the drupal code calling back into your code, which then can call back into drupal code.
A normal PHP program works like this:
- The web server picks the PHP file to run based on the URL.
- The PHP file runs. It can optionally call PHP standard library functions and include other PHP files for extra functionality.
- As the PHP file runs, whatever it outputs is simultaneously1 output to the web browser.
A Drupal website works like this:
- The web server picks the PHP file to run based on the URL That PHP file is (almost) always Drupal core's
index.php - The PHP file runs. (
index.php). It includes a bunch of include files which define helper functions and settings, it connects to the database (which is required) and pulls more settings out of the database, it includes even more files (themes and modules) which define yet more helper functions and callbacks, and finally it decides which function, in all of the combined tens of included files that should be run - It calls that function, which is called a "menu callback". This "menu callback" is the first chance that you have to run your own code.
- Your menu callback runs. It can optionally call PHP standard library functions, include other PHP files for extra functionality, or call any Drupal standard library functions.
- As the menu callback runs, anything that it outputs is sent to the browser, just like in the raw PHP file case above.
- But the Drupal Way is for the menu callback to return a string. That string then gets plugged into a template page for the site (which is one component of a theme), and the template page with the string plugged into the right spot gets sent back to the browser.
There are some advantages of the Drupal way of doing things:
- It's consistent. For example, if you always use the "correct" way to print out usernames, changing the style of those printed usernames in one place changes them across the entire site.
- It's integrated.
- It's convenient.
But there are some disadvantages:
- It's slow(er).
- It uses memory.
1Probably not true in most cases, but good enough for this comparison
