Lots of PHP errors when trying to autoload classes

Hello, apologies if this isn’t in the right place or displays some obvious lack of knowledge with PHP—that’s me. The following series of things are causing lots of errors on my Kirby site, and I was hoping someone could help me identify what’s going on. The same code used elsewhere (on non-CMS sites, just via plain old web hosting) works fine, so I figure I’m encountering some conflict with Kirby itself.

In my default.php template in site > templates I’m including a file called arena.php via the following: include 'arena.php';

Kirby is able to find this file, it’s in the root directory (same place as index.php) but then returns a host of errors originating from another include when the file tries to run its course:

The lib folder is also in root, same place as arena.php. The list of errors on the left goes on for some time. Does this have something to do with the composer.json file (which is also in root)? The contents of that file are:

{
	"name": "arenahq/arena-php",
	"description": "PHP interface to the Arena API",
	"homepage": "https://github.com/arenahq/arena-php",
	"license": "MIT",
	"type": "library",

	"autoload": {
		"psr-4": {"": ""}
	}
}

As mentioned, this same code works on other (non-Kirby) websites. I’m clueless, and any help is greatly appreciated.

I think it would make more sense, to require your library in a plugin instead of in a template. That’s not the place to require additional scripts.

1 Like

That makes much more sense, thanks for the response. I moved all the files in question into a plugin folder, and made the call to include them (include 'arena.php') in the plugin’s index.php file. I think in the end the error was the file path. I changed /lib to include '/../../../../../../lib/' . $class . '.php'; and now the error is gone.

edit: Nevermind. Sorry for the multiple edits, but just quickly went through a rollercoaster of results. I believe the include 'arena.php' is successfully being called via its inclusion in a plugin’s index.php file, but when I try to then interact with that via $arena = new Arena(); and eventually $channel = $arena->get_channel($slug, array('page' => $page, 'per' => $per)); I get the same error:

I now realize that /../ method doesn’t make any sense. Is this simply an error in finding the directory though? How can I find out what Kirby considers to be the file path to get to that lib folder, now located inside the plugin’s folder?

Sorry this is convoluted, but I think my question remains simple: is there anything about Kirby that would coming into conflict with this PHP library I’m using? The identical code hosted elsewhere works.

  • I’m doing include 'arena.php'; in a plugin, which works
  • I’m calling on it via $arena = new Arena(); in a template file, which works, I can even begin to manipulate the objects it gives me once configuring it
  • If I try to manipulate it enough to call upon parts of the library in the lib folder, I receive the error (similar to the ones I posted above: “No such file or directory”)
  • If I do include 'lib/nameofafilethatexists.php'; in the template file, I can successfully find it (meaning the directory exists and is findable)
  • This makes me think the “file” doesn’t exist, which is maybe a problem with the $class portion of that error, as Kirby is looking for lib/$class.php. The $class comes from an object I’m calling in the template file, which I can successfully print and view.

So basically, my questions are now:

  • is there any way I can test what $class is being passed to it? If I try and do print_r($class); Kirby’s debug just shows me the code I wrote, it won’t actually print the variable. With debug mode off (expecting that it would print it), I get the black text on white background Kirby error message.
  • knowing that this exact code works elsewhere, is there something inherent to Kirby that would interfere with what’s going on?

Thanks so much for any help.

After reading some more it seems like the best thing to would be to utilize this: https://getkirby.com/docs/guide/plugins/plugin-setup-autoloader, as it seems like there’s some conflict with Symfony when using this code:

function autoloader($class) {
include 'lib/' . $class . '.php';`
}
spl_autoload_register('autoloader');

Could anyone provide some advice on how to convert this: https://github.com/arenahq/arena-php into a plugin?

I tried to install the library with composer yesterday but failed with an error. It looks as if the package is not available on packagist. But if you look at when it was last updated, you’ll see that this library is no longer under active development.

Edit: Try disabling Whoops in your config: https://getkirby.com/docs/reference/system/options/whoops

Thanks for the suggestions/help. I disabled Whoops and now just have a completely blank screen (with nothing in the DOM).

I can’t tell what, but it seems like structurally there is just something conflicting between how Kirby operates and this library—it works perfectly fine elsewhere on the web. Maybe it is the use of $page and operators like that?

You are not giving any context where and how you are using it, but if you define $page like this

$page = $arena->set_page(); 

And then you also have Kirby code like

echo $page->title();

This will of course conflict. Use other variables to prevent this.

$page, $pages and $site are special variables in Kirby that shouldn’t be used for other stuff.

I arrived at this as well. Turns out this was simply the problem. Thanks.