Dependency Injection custom interface Class not found

Im having problem with getting a custom interface to be found.

Could not resolve AddressBookEntryCache! Class "AddressBookEntryCache" does not exist. File: /var/www/html/lib/private/AppFramework/Utility/SimpleContainer.php

Above is the error I get. This is my code:

<?php
declare(strict_types=1);

use OCP\AppFramework\Http\DataResponse;
use OCP\Http\Client\IClientService;

class AddressBookEntryCache implements IAddressBookEntryCache
{

	private static $cache;
	private static int $timestamp;

	private IClientService $clientService;

	public function __construct(IClientService $clientService) {
		$this->clientService = $clientService;
	}

    public function getCache(): DataResponse
    {

		return new DataResponse("insert logic here when things work");
    }
}

<?php
declare(strict_types=1);

interface IAddressBookEntryCache {

	public function getCache(): \OCP\AppFramework\Http\DataResponse;

}

<?php
declare(strict_types=1);

namespace OCA\SDK\AppInfo;

use AddressBookEntryCache;
use IAddressBookEntryCache;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Http\Client\IClientService;
use Psr\Container\ContainerInterface;

class Application extends App implements IBootstrap {
	public const APP_ID = 'sdk';

	public function __construct(array $urlParams = []) {
		parent::__construct(self::APP_ID, $urlParams);

	}

	public function register(IRegistrationContext $context): void
	{

		$context->registerService(IAddressBookEntryCache::class, function (ContainerInterface $c): AddressBookEntryCache {
			return new AddressBookEntryCache($c->get(IClientService::class));
		 });

		/*$context->registerService(AddressBookEntryCache::class, function (ContainerInterface $c): AddressBookEntryCache {
			return $c->get(AddressBookEntryCache::class);
		});*/
		$context->registerServiceAlias(IAddressBookEntryCache::class, AddressBookEntryCache::class);
	}

	public function boot(IBootContext $context): void
	{
		// TODO: Implement boot() method.
	}
}

I’ve tried a couple things in Application.php, thats why it’s commented out, and in AddressBookEntryCache.php all the logic is commented out.

I’ve followed Dependency injection — Nextcloud latest Developer Manual latest documentation

But can’t get it to work.
I use Nextcloud 27 and PHP 8.1.2

Hi @jsaasta :wave:

I think you have to define a namespace for your AddressBookEntryCache.php and IAddressBookEntryCache.php files.

For example :

IAddressBookEntryCache.php

<?php

namespace OCA\<appname>\Interfaces;

// code

class IAddressBookEntryCache
{
  // code
}

Notice : I guess your class is in this folder lib/Interfaces for the namespace.

AddressBookEntryCache.php

<?php

namespace OCA\<appname>\AddressBook;

use OCA\<appname>\Interfaces\IAddressBookEntryCache;
// code

class AddressBookEntryCache
{
  // code
}

Notice : I guess your class is in this folder lib/AddressBook for the namespace.

Just, one thing ! I see you use an old syntax to declare your properties :

class AddressBookEntryCache implements IAddressBookEntryCache
{

	private static $cache;
	private static int $timestamp;

	private IClientService $clientService;

	public function __construct(IClientService $clientService) {
		$this->clientService = $clientService;
	}
        
        // code

You can use the constructor property promotion if you are using the version 8.0 of PHP.

For example :

class AddressBookEntryCache implements IAddressBookEntryCache
{

	private static $cache;
	private static int $timestamp;

	public function __construct(private IClientService $clientService) {
	}
        
        // code

And voilà :wink:

1 Like

I find this a very nice summary and is well written!

1 Like

Thanks :blush:

I don’t know if my answer can fix his/her problem ^^’

Thank you kind sir for your explanation.
It was the namespace I was missing. Would never figure that out myself (being new to PHP and no warning or error about it :smile: )

Thank you for a quick and well written summary. Noticed the project actually was using version 7.4, but when I type php --version I have PHP 8.1.2-1ubuntu2.14 (cli).
Is it just as easy to change in composer.json under

	"config": {
		"allow-plugins": {
			"composer/package-versions-deprecated": true
		},
		"platform": {
			"php": "7.4"
		}
	},

Change platform: php 7.4 to 8.1?

Thanks

The setting in the composer just sets the compatibility level. So, if you had a newer PHP version, it will restrict the dependencies to those dependency versions that are in fact satisfying the (overridden) PHP version.

You are not setting the PHP version in some sense. It is just the version that is in use. Keep in mind, this is also true for the users of the app. So, if you would require a minimum PHP version of 8.0 that would be your decision to make your life easier and use the new features. You can mark this in the file app/info.xml that has a PHP version restriction. This will make the NC updater ignore this individual version from the app store if the PHP was not in the correct version present.

Short answer: You cannot enforce the PHP version you must be compatible with it.

You’re welcome !! :wink:

As christianlupus said : you can use your current version of php. Composer inform users the version you should use. It’s not like npm for comparison.

To add to what christianlupus said, you can define the version of your php in the info.xml look at on this section from the dev documentation : App metadata — Nextcloud latest Developer Manual latest documentation

For PHP, I don’t have advice for you, I have just french moocs or tutos ^^’

@christianlupus : It’s not our technical solution for these subjects ?

One thing I want to add to this from my experience: Although 8.1 is the oldest supported PHP version today, you have to consider supporting even older ones. State right not is that LTS ubuntu (20.04 LTS) still has 7.4 as default PHP version until 2025! That means you might geht strange messages from users complaining about errors/warnings by their ancient PHP installations in the worst case and if you are not having a close eye on things.

Just a warning as it happened to me recently.

1 Like