Can someone please tell me if you require to include a class before you can instantiate it with the include() function, as if i include say error.php in mysqlconn.php and then create a new file login.php and include both mysqlconn.php and error.php it complains as shown:
Fatal error: Cannot redeclare class errorlogging in C:\Intranet\php\errorLogging\ErrorLogging.php on line 8
is this because it appears in both files, do i even need to include or should i be doiong it some other way?
Again Thanks in advance!
(Not sure if this has posted twice as i had problems - sorry if it did)
Help with Classes
Moderator: General Moderators
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Just use include_once() instead of include() to include the file - that way if it has already been included once already the script won't bother trying to include it again.
Mac
Mac
the class definition needs to be known before you can use the class.
If you have e.g. three filesby including create.php also classdef.php has been loaded into the context of main.php
But maybe you're also interested in http://www.php.net/manual/en/function.require-once.php
If you have e.g. three files
Code: Select all
// classdef.php
<?php
class CTest
{
var $m_prop;
}
?>Code: Select all
// create.php
<?php
include ('classdef.php');
function getClass()
{
$ret = new CTest;
$ret->m_prop = rand(0,100);
return $ret;
}
?>Code: Select all
// main.php
<?php
include('create.php');
$instance = getClass();
?>But maybe you're also interested in http://www.php.net/manual/en/function.require-once.php