External Files
Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 1 of 91.
External files let you organize your classes in separate PHP files and include them into your main program using require_once.
Create a separate PHP file called my_class.php
<?php
class MyClass {
// class body
}Include the file into your main file using require_once
<?php
require_once 'my_class.php';Now you can use the class in your main file
$obj = new MyClass();
echo get_class($obj);Output:
MyClassThe require_once 'my_class.php'; statement connects the my_class.php file to your program. The filename must include the .php extension and be wrapped in quotes. require_once ensures the file is loaded only once, even if the statement appears multiple times.
Challenge
MediumYou are given PHP files (my_class.php and driver.php). Add the correct require_once statement in the driver.php file to connect the files and use the class from the external file!
Cheat sheet
Use require_once to include external PHP files into your main program:
require_once 'my_class.php';The filename must include the .php extension and be wrapped in quotes. require_once ensures the file is loaded only once, even if the statement appears multiple times.
Example of using a class from an external file:
<?php
require_once 'my_class.php';
$obj = new MyClass();
echo get_class($obj); // Output: MyClassTry it yourself
<?php
// TODO: Add the require_once statement to include my_class.php
$testCase = trim(fgets(STDIN));
$obj = new MyClass();
echo "Test completed\n";
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe $this KeywordMethodsPropertiesConstructor (__construct)Destructor (__destruct)Recap - Simple Calculator4Inheritance
Basic InheritanceThe parent:: KeywordMethod OverridingThe final KeywordAbstract ClassesRecap - Employee Hierarchy7Encapsulation
Public, Protected, PrivateAccess Modifiers in DepthGetters and SettersInformation HidingConstructor Promotion (8.0)Recap - Student Records System2Namespaces & Autoloading
Introduction to NamespacesThe use KeywordPSR-4 Autoloading StandardComposer AutoloaderRecap - Organized Project5Interfaces & Contracts
Introduction to InterfacesImplementing InterfacesMultiple Interface ImplementInterface vs Abstract ClassType Hinting with InterfacesRecap - Shape Calculator8Magic Methods
Magic Methods Introduction__toString & __debugInfo__get, __set, __isset, __unset__call & __callStatic__clone & Object Cloning__serialize & __unserializeRecap - Custom Collection11Type System & Error Handling
Type DeclarationsNullable TypesUnion & Intersection TypesException ClassesCustom Exception HierarchyTry, Catch, FinallyRecap - Form Validator14Project: Library Management
Project OverviewBook and User Classes3Class Properties
Instance vs Static PropertiesConstants in ClassesStatic Methods & PropertiesPrivate & Protected PropertiesReadonly Properties (PHP 8.1)Recap - Bank Account Manager6Polymorphism
Method Overriding RevisitedPolymorphism via InterfacesType Hinting & Union TypesLate Static BindingRecap - Payment Processor9Traits
Introduction to TraitsUsing Multiple TraitsTrait Conflict ResolutionAbstract Methods in TraitsTraits vs Inheritance12Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern