Help with Classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
liquidchild
Forum Newbie
Posts: 9
Joined: Sun Nov 03, 2002 6:25 am

Help with Classes

Post 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)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Post Reply