Recent Posts

How to solve: Fatal error: Cannot redeclare [function name]

This means you're either using the same function/class name twice and need to rename one of them, or it is because you have used require or include where you should be using require_once or include_once.
When a class or a function is declared in PHP, it is immutable, and cannot later be declared with a new value.
Consider the following code:
class.php
<?php

class MyClass
{
    public function doSomething()
    {
        // do stuff here
    }
}
index.php
<?php

function do_stuff()
{
   require 'class.php';
   $obj = new MyClass;
   $obj->doSomething();
}

do_stuff();
do_stuff();
The second call to do_stuff() will produce the error above. By changing require to require_once, we can be certain that the file that contains the definition of MyClass will only be loaded once, and the error will be avoided.

No comments:

Post a Comment