Help calling Namespace function

I have an extra function I put inside of lib/extras.php. I’m having a few issues.

If I call the function with the namespace inline on my local dev environment it works.
<?php echo Roots\Sage\Extras\myfunction(); ?>

But if I declare the namespace at the top of the page using the use function. I get an error to undefined function.

Any ideas?

If you declare the namespace at the top of the file you don’t have to use it again when you call the function, since both your local file and the function live in the same space!

Yeah I’m not using it when I call my function with the namespace declared at the top of the template. Just echo myfunction()

Instead of namespacing your file, use the use Roots\Sage\Extras\myfunction up top, this will give access to that specific function while keeping the namespace clean. This is useful especially when you have to use components from different namespaces( models, controllers etc etc)

I tried that as well and got the same Call to Undefined Function error as before. Is there anything maybe I’m not doing when declaring my function?


namespace Roots\Sage\Extras;

use Roots\Sage\Setup;

function myfunction(){
//do stuff

Which version of sage are you using? Sage9 uses PSR-4 which means the namespace has to reflect your folder structure as well or the file won’t be loaded by composer. If you’re still on sage8, as long as you have something like use function Roots\Sage\Extra\myfunction you should be good to go!