Menu
CoddyTech

Modules and first run

Lesson 1 of 48 in Coddy's PHP for Ruby Developers course.

Ruby require_relative loads a file relative to the current source file and requires it once. PHP uses require_once with __DIR__ to anchor the path. Ruby files need no PHP opening tag; PHP CLI is not a web request, and helper modules should not print during loading.

PHP CLI files start with an opening PHP tag. Keep reusable declarations in modules and output in the entry script. require_once loads a file only once; __DIR__ anchors its path to the current file instead of the working directory. These courses use PHP 8.3 syntax and standard CLI input, not a web framework. Strict scalar calls are requested with declare(strict_types=1) in the caller file.

Ruby program

puts "hello CLI"

Source output:

hello CLI

PHP program

demo.php

<?php
declare(strict_types=1);
echo "hello CLI\n";

PHP output:

hello CLI
challenge icon

Challenge

Easy

Run unchanged. The main script loads Functions.php relative to its own directory and prints PHP is ready.

The supplied main script reads one JSON value from standard input, checks the named public APIs, and prints the result. Implement the module declarations without printing; do not replace the launcher or read input from a module. Input satisfies the stated types and bounds.

Edit Functions.php. main.php is the locked entry file. Dependencies use require_once __DIR__ with a relative filename.

Try it yourself

<?php
declare(strict_types=1);
require_once __DIR__."/Functions.php";
function ensure(bool $ok,string $message):void{if(!$ok)throw new RuntimeException("API: ".$message);}

$input=json_decode(trim(stream_get_contents(STDIN)),true,512,JSON_THROW_ON_ERROR);
ensure(welcome()==="PHP is ready","welcome");
echo welcome(),"\n";

All lessons in PHP for Ruby Developers

Practice on your own: Online PHP compiler