Lesson 1.4: A script with the kernel loaded
The fastest way to learn the kernel's classes is to call them. bin/php/ezexec.php loads the kernel (settings,
database, autoloads) and then runs any PHP file. In this lesson learners create the extension folder they will use
for the rest of the course, and write a script that reads a setting and a node.
At a glance
| Course | Developers |
| Module | Module 1: The development installation |
| Duration | 45 minutes (10 presenting, 35 exercise) |
| Level | Beginner |
| Audience | PHP developers new to Exponential |
| Prerequisites | Lesson 1.3; a text editor |
Learning objectives
After this lesson, learners can:
- create an extension folder
extension/mysite_helloand put it under Git; - write a script that uses
eZINI,eZCLIandeZContentObjectTreeNode; - check a PHP file with
php -lbefore running it; - run a script with
php bin/php/ezexec.php <file>and read its output.
Trainer's notes
How to present it
Source: Your first hour as a developer, exercise 6.
- The course extension (3 minutes). Every learner builds one extension through the course,
mysite_hello. It is not active yet; module 7 switches it on. A script run withezexec.phpneeds no activation. - Three classes (4 minutes).
eZINI::instance( 'site.ini' )reads settings with every override applied;eZCLI::instance()writes to the terminal;eZContentObjectTreeNode::fetch( $nodeID )loads a node (a place in the content tree). - Checking first (1 minute).
php -lis the compile check before anything else. - Running (2 minutes).
php bin/php/ezexec.php extension/mysite_hello/scripts/hello.php.
Common questions
What is the difference between a node and an object? An object is the content (with versions and translations); a node is one of its places in the tree. Most templates and fetches work with nodes. Module 4 goes deeper.
Is a script run this way allowed to change content? It runs with the kernel's full rights. In this course, scripts only read until module 9.
How do I pass options to the script? Scripts that need options are written as console commands; lesson 9.2 shows how.
Pitfalls
- A file outside the installation. Keep scripts inside the extension; paths in examples are relative to the installation root.
- Running as root. Add nothing: work as the learner's own user. As root, scripts stop with a warning unless
--allow-root-useris given. - Node 2 is not always the front page. On the training package node 2 is the top of the content tree. Learners can check the ID in the administration.
Hands-on exercise
On the development installation, from its root folder.
Create the extension folder and its Git repository:
mkdir -p extension/mysite_hello/scripts git -C extension/mysite_hello initCreate
extension/mysite_hello/scripts/hello.phpwith this content:// Run: php bin/php/ezexec.php extension/mysite_hello/scripts/hello.php $cli = eZCLI::instance(); $ini = eZINI::instance( 'site.ini' ); $cli->output( 'Site name: ' . $ini->variable( 'SiteSettings', 'SiteName' ) ); $cli->output( 'Active extensions: ' . count( $ini->variable( 'ExtensionSettings', 'ActiveExtensions' ) ) ); $node = eZContentObjectTreeNode::fetch( 2 ); if ( $node instanceof eZContentObjectTreeNode ) { $cli->output( 'Node 2: ' . $node->attribute( 'name' ) . ' (class ' . $node->attribute( 'class_identifier' ) . ')' ); $cli->output( 'Children: ' . $node->subTreeCount( array( 'Depth' => 1 ) ) ); } else { $cli->error( 'Node 2 was not found' ); }Check the syntax:
php -l extension/mysite_hello/scripts/hello.php.Run it:
php bin/php/ezexec.php extension/mysite_hello/scripts/hello.php.Compare the site name with
php bin/php/console exp:ini get site.ini/SiteSettings/SiteName.Change the script to fetch the node ID of an article you can see in the administration, and run it again.
Add the line
eZDebug::writeError( 'Test error from hello.php', 'mysite_hello' );at the end, run the script, and find the line invar/log/error.log.Remove the line from step 7.
Commit the script in the extension's own repository:
git -C extension/mysite_hello add scripts/hello.php git -C extension/mysite_hello commit -m "Added: First script that reads settings and a node"
Expected result
- Step 3 prints
No syntax errors detected. - Step 4 prints the site name, the number of active extensions, the name and class of node 2 and its number of children.
- Step 7:
var/log/error.logends with a line containingTest error from hello.phpand the labelmysite_hello. - Step 9:
git -C extension/mysite_hello log --onelineshows one commit. - The trainer checks the output of step 4 on two learners' screens.
Quiz
- What does
bin/php/ezexec.phpdo before it runs your file? - Which class reads settings with every override applied?
A.
eZCLIB.eZINIC.eZDBD.eZScript - What is the difference between a content object and a node?
- Which command checks a PHP file's syntax without running it?
- Why does the course extension live in a Git repository of its own and not in the kernel's?
Answers
- It loads the kernel: settings, database connection and autoloads.
- B.
eZINI::instance( 'site.ini' ). - The object is the content with its versions and translations; a node is one place of it in the content tree.
php -l <file>.- Extensions are separate repositories; the kernel is updated through Composer and Git, and your work is committed in your own extension (Setting up a development installation).
Further reading
- Your first hour as a developer: six short exercises around settings, templates, debug output and scripts.
- The tools you need: PHP, Composer, Git, an editor and the browser's developer tools.
Related pages
- Module 1: The development installation
- Developers: course overview
- Trainers: start here
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.