Page 1 of 1

include / require runs constructor

Posted: Wed Apr 21, 2004 11:17 am
by allelopath
the include and require directives appear to cause the constructor of the specified file to run.

I expected to do this:

include 'myClass.php';
.
.
.
$myClass = new myClass();

but this actually causes 2 instantiations of myClass
to get what i want, i move the include statement to where the $myClass = new myClass statement is (and remove the $myClass statement)

its not like the C include, nor is it like a Java import.
no question, i guess, just seems odd to me.
comments welcome, though.

Posted: Wed Apr 21, 2004 11:44 am
by JAM
It should not (depending on the script that is). Can you display more code for us?

Can you verify that this alone, gives you 2 instances?:

Code: Select all

<?php
include 'myClass.php';
echo 'foo';
$myClass = new myClass(); 
?>
If not, you need to look elsewhere. If so, check the myClass.php file for more information...

Posted: Wed Apr 21, 2004 11:54 am
by allelopath
yes, your code sample gives 2 instances

Posted: Wed Apr 21, 2004 11:58 am
by allelopath
oh, i see the problem.
it is in myClass.
thanks for your help, i wouldn't have looked otherwise.

Posted: Wed Apr 21, 2004 11:59 am
by JAM
Glad it helped.