PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 16, 2022

[FIXED] Laravel 8 Custom Helper function PHP Fatal error: Cannot redeclare functionName() previously declared in C:(patth)Helpers.php

 February 16, 2022     laravel, php     No comments   

Issue

I've a Custom function in App\Helpers.php. When I use the function in if statement in a blade file. I see error in Laragon error logs.

PHP Fatal error:  Cannot redeclare CheckInvalidPlan() (previously declared in C:\laragon\www\projectname\app\Helpers.php:6) in C:\laragon\www\projectname\app\Helpers.php on line 6

However things works as expected. But why is this error causing and how can I fix it?

#Update

This is my function in Helpers.php

function CheckInvalidPlan($id)

{

    if (Plan::find($id) == null)
    {
        return true;
    }

}

This is my if statement in Controller.

if (CheckInvalidPlan ($request->plan_id))
        {
            return back()->with('invalid', 'Invalid membership plan spesified.');
        }

Solution

You can bypass this error by checking if your function already exists:

if(! function_exists('CheckInvalidPlan')) {
    function CheckInvalidPlan($id)
    {
        if (Plan::find($id) == null)
        {
            return true;
        }
    }
}

That's how Laravel helpers are declared:

if (! function_exists('today')) {
    /**
     * Create a new Carbon instance for the current date.
     *
     * @param  \DateTimeZone|string|null  $tz
     * @return \Illuminate\Support\Carbon
     */
    function today($tz = null)
    {
        return Date::today($tz);
    }
}

However, a cleaner approach would be to understand why your helpers file is loaded twice.

It is hard to tell you exacly where the error could be, however you should inspect all your classes, the app\Helpers.php file should never be required manually. It should be autoloaded by composer, as explained in this answer (thanks N69S).



Answered By - Anthony Aslangul
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing