I solved a similar problem, based on great help from the Joomla forum:
https://forum.joomla.org/viewtopic.php?f=831&p=3677225
Setting the namespace with a use statement was not working for me.
Here is the final standalone example:
namespace some\custom\namespace;
define('JOOMLA_MINIMUM_PHP', '7.2.5');
define('_JEXEC', 1);
// Just to show your example can live in any sub-folder of "its own"
define('JPATH_BASE', __DIR__ . "/../../../../");
include_once JPATH_BASE . "/includes/defines.php";
include_once JPATH_BASE . "/includes/framework.php";
$container = \Joomla\CMS\Factory::getContainer();
$container->alias('session.web', 'session.web.site')
->alias('session', 'session.web.site')
->alias('JSession', 'session.web.site')
->alias(\Joomla\CMS\Session\Session::class, 'session.web.site')
->alias(\Joomla\Session\Session::class, 'session.web.site')
->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');
$app = $container->get(\Joomla\CMS\Application\SiteApplication::class);
\Joomla\CMS\Factory::$application = $app;
\JLoader::register('JNamespacePsr4Map', JPATH_LIBRARIES . '/namespacemap.php');
(new \JNamespacePsr4Map)->load();
$app->createExtensionNamespaceMap();
use \Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
// Get data for the logged-in user
$user = \Joomla\CMS\Factory::getApplication()->getSession()->get('user');
if ($user->id != NULL) {
// User logged in, get the custom fields data
$customFields = FieldsHelper::getFields('com_users.user', $user, 0);
echo "<pre>USER:" . PHP_EOL;
var_dump($user);
echo PHP_EOL . "<hr />CUSTOMFIELDS:" . PHP_EOL;
var_dump($customFields);
echo "</pre>";
}
else {
echo "User not logged in.";
}
but giving me error
Please edit your question to include the exact error message that you are receiving.$article
and$fields
? It is important that you narrow your support question to the earliest point of error. Telling us what is working as desired helps volunteers to better understand your problem and confidently suggest a resolution.