Skip to main content

Load Classes Automatically In OOP PHP with an Autoloader - #70


Published: August 16, 2021 | DevDrawer


Estimated Reading Time
2 minutes
Video Duration
21:26 minutes

If you are ever working with OOP in PHP you need to know about autoloading your classes. You can do this with or without Composer. In this tutorial, we will not be using Composer. Instead, I will show you a quick way to actively look at a folder for specific classes and autoload them so they can be used throughout your application without having to review or include new files every time you create them.

This solution is built into frameworks like Laravel, but if you ever need to build a simple OOP application, this is the best way to minimize code and time.

We will be using the "spl-autoload-register" PHP function.

00:00 Intro

00:48 Livestream Plug

01:28 The Old Way

05:11 Create and Require Our Autoloader

07:51 Building Example Classes You Can Use

12:01 Create An .htaccess File

13:29 Testing Our Code and Autoloader

Download the tutorial files here: https://github.com/thedevdrawer/php-oop-autoloader

Music Playlist Livestream: https://youtu.be/uCJBki94q2I

Become a Patreon to show support and get exclusive access to content: https://www.patreon.com/devdrawer

Applications Used

Visual Studio Code v1.43: https://code.visualstudio.com/

WAMP v3.2.0: http://www.wampserver.com/en/

Computer Hardware:

Windows 10 Pro: https://amzn.to/3q5TyZw (Amazon)

Samsung 500GB External SSD: https://amzn.to/2ViAAAF (Amazon)

Samsung 970 EVO Plus SSD 1TB - M.2 NVMe: https://amzn.to/36jDwDy (Amazon)

Rosewill Hive-750S R750W PSU: https://amzn.to/36jiDbs (Amazon)

Phanteks Eclipse Tempered Glass Case: https://amzn.to/39qou0B (Amazon)

MSI Arsenal Gaming AMD Ryzen Motherboard: https://amzn.to/36hpAKm (Amazon)

MSI Gaming GeForce RTX 2060 Super 8GB: https://amzn.to/36hMyRm (Amazon)

LG 34UM69G-B 34-Inch 21:9 UltraWide IPS Monitor: https://amzn.to/2HSRxyI (Amazon)

Corsair 64GB (4 x 16GB) DDR4 Memory: https://amzn.to/3mlmQkI (Amazon)

AMD Ryzen 7 2700X Processor: https://amzn.to/3qdQIBF (Amazon)

Dell XPS 15 9500 15.6" Laptop / Windows: https://amzn.to/3fKEz2w (Amazon)

Lenovo ThinkPad T15 15.6" Laptop / Linux: https://amzn.to/2Jnmn33 (Amazon)

Camera:

Fujifilm X-T3: https://amzn.to/3llKBb5 (Amazon)

Fujinon XF18-55mmF2.8-4: https://amzn.to/3mfTkNb (Amazon)

Audio:

Audio Technica AT2020: https://amzn.to/2VgxU6K (Amazon)

Blue Snowball iCE: https://amzn.to/3lo1jGC (Amazon)

DISCLAIMERS:

I am a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.