php - Fatal error: Cannot redeclare class Database -


i have fetal error message :

fatal error: cannot redeclare class database in c:\wamp\www\pets_new\lib\database.php on line 3

require_once("lib/message.php"); require_once("lib/user.php"); 

and connect database class

class message

<?php  require('database.php');  class message{ 

class user :

<?php  require('database.php');  class user{ 

you include 2 files in single "run". think of this: included files put php create 1 big script. every include or require fetches file, , pastes content in 1 big script.

the 2 files including, both require same file, declares database class. means big script php generates looks this:

class message {} class database {}//required message.php class user {} class database {}//required user.php 

as can see class database declared twice, hence error.
now, quick fix can replacing require('database.php'); statements with:

require_once 'database.php'; 

which checks if particular file hasn't been included/required before. if has been included/required before, php won't require again.
more definitive and, imho, better solution register autoloader function/class method, , let code take care of business.

more on how register autoloader can found in docs. if go down route, you'd want take @ coding standards concerning class names , namespaces here. if conform standards, don't have write own autoloader, , can use universal class loader symfony2, or other framework subscribes php-fig standards (like codeignitor, zend, cake... name it)


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -