How can i call a class from another file in php?

Like this article? We recommend

Importing Classes from a Separate File

Although it's useful to modularize our code the way we have, it's best if every page that refers to events does it through Event objects. So for maintenance purposes, it's best to move the class definition into a separate file that we can call from different pages. To start, create a new file—I'll call mine objects.inc, and I'll place it in the same directory as showevent.php—and save it. Place the class definition in that file, as shown in Listing 5.

Listing 5—A Separate Class Definition File [objects.inc]

Notice that the entire section is enclosed in the delimiters.

To actually use the object, we need to make the class definition available from within the page. To do that, we can use the require[] function, as shown in Listing 6.

Listing 6—Making the Class Definition Available [showevent.php]

Chủ Đề