Class autoloading

Class Autoloading

Encapsulated plugins allow you to Autoload classes based on 2 scenarios.

  • Where the class follows PSR4 and the filename is the same as the class name
  • Where the filename is different to the class name

PSR4 based classes

Encapsulated plugins manage these automatically, by placing the classes in specific diectories.

e.g for admin I can place a class in the plugins admin/includes/classes/ directory

As an example lets use the current DisplayLogs plugin.

In its admin/includes/classes/ directory lets add a new class called Cache.php I’ve named this to see how it doesn’t interfere with the current cache class that Zen Cart uses.

This class looks like

<?php

namespace Zencart\Plugins\Admin\DisplayLogs;

class Cache
{
    public function test()
    {
        echo 'FOOOOO';
    }
}

now in the admin/display_logs.php file, I can call this after the line require 'includes/application_top.php';

$cache = new Cache();
$cache->test

Note: we also need to add

use Zencart\Plugins\Admin\DisplayLogs\Cache;

to the beginning of the file.

When the DisplayLogs plugin is installed we should see the FOOOOOoutput when navigating to its menu entry.

Custom class filenames

When the filename of a class does not match the class name, we need to do a bit more work.

Again lets create a new class file in the plugin’s admin/includes/classes directory.

Lets call this file class.some_weird_class_filename.php with the contents

<?php

namespace Zencart\Plugins\Admin\DisplayLogs;

Class myClass
{
    public function test()
    {
        echo 'BARRRRR';
    }
}

now in the admin/display_logs.php file, I can add this call after the line `require ‘includes/application_top.php’;

$psr4Autoloader->setClassFile('Zencart\Plugins\Admin\DisplayLogs\myClass', $filePathPluginAdmin['DisplayLogs'] . 'class.some_weird_class_filename.php');
$myClass = new Zencart\Plugins\Admin\DisplayLogs\myClass();
$myClass->test();

This should output 'BARRRRR' on the Display logs page in admin




Still have questions? Use the Search box in the upper right, or try the full list of FAQs. If you can't find it there, head over to the Zen Cart support forum and ask there in the appropriate subforum. In your post, please include your Zen Cart and PHP versions, and a link to your site.

Is there an error or omission on this page? Please post to General Questions on the support forum. Or, if you'd like to open a pull request, just review the guidelines and get started. You can even PR right here.
Last modified July 26, 2023 by Chris Brown (4367cfd).