PHP - Custom Type Class include

Introduction

Consider the following class

<?php 
     class Customer { 
        private $id; 
        private $firstname; 
        private $surname; 
        private $email; 

        public function __construct( 
            int $id, 
            string $firstname, 
            string $surname, 
            string $email 
        ) { 
            $this->id = $id; 
            $this->firstname = $firstname; 
            $this->surname = $surname; 
            $this->email = $email; 
        } 
     } 
?>

Suppose we save it to current folder with file name Customer.php.

To use it in another file, use the require command.

require_once __DIR__ . '/Customer.php'; 
$customer1 = new Customer(1, 'John', 'Doe', 'a@mail.com'); 

You do not need to include the files every single time.

Once you include them, PHP will know where to find the classes, even though your code is in a different file.

Related Topics