1

I created an helper and I try to use it in one of my controllers,but I got an error, and I am not sure why.

//StringHelper.php
namespace App\Helpers;

class StringHelper
{
    public function example($str1){
        //CODE
    }
}


//config/app.php
'aliases' => [
    'StringHelper' => App\Helpers\StringHelper::class,
]


//In controller 
use StringHelper;

$percentage = StringHelper::example($title);

Non-static method App\Helpers\StringHelper::example() should not be called statically

1 Answer 1

1

Because the method example($str1) is not static, you need to call it by instance.

I think you are calling other instance's methods in example, so the simple way is call the method by instance.

$helper = new StringHelper();
$percentage = $helper->example($title);

Or you need to defined all those methods to static.

Sign up to request clarification or add additional context in comments.

1 Comment

I also change 'use', with use App\Helpers\StringHelper, and it work, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.