Fatal error: Cannot redeclare spl_autoload_register()

Asked

Viewed 1,411 times

0

<?php

function spl_autoload_register($class){require_once"{$class}.class.php";}

class ConDB
{
    private static $cnx;
    private function setConn()
    {
        return
        is_null(self::$cnx)?
            self::$cnx=new PDO("mysql:host=localhost;dbname=***","***","***"):
        self::$cnx;
    }
        public function getConn()
    {return $this->setConn();}
}
$cnx=new ConDB;

1 answer

1


Because you are simply trying to create/redeclare a function with the name of a existing native function:

function spl_autoload_register($class){require_once"{$class}.class.php";}

If the intention is to use spl_autoload_register, it would be right to do this:

function meu_autoloader($class) {
    require_once"{$class}.class.php";
}

spl_autoload_register('meu_autoloader');

Or use closure (anonymous PHP function) directly:

spl_autoload_register(function ($class) {
    require_once"{$class}.class.php";
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.