Page 1 of 1

Help with Classes

Posted: Fri Jan 17, 2003 6:28 am
by liquidchild
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)

Posted: Fri Jan 17, 2003 6:39 am
by twigletmac
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

Posted: Fri Jan 17, 2003 6:48 am
by volka
the class definition needs to be known before you can use the class.
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();
?>
by 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