include / require runs constructor

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
allelopath
Forum Commoner
Posts: 34
Joined: Tue Mar 16, 2004 5:21 am

include / require runs constructor

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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...
allelopath
Forum Commoner
Posts: 34
Joined: Tue Mar 16, 2004 5:21 am

Post by allelopath »

yes, your code sample gives 2 instances
allelopath
Forum Commoner
Posts: 34
Joined: Tue Mar 16, 2004 5:21 am

Post by allelopath »

oh, i see the problem.
it is in myClass.
thanks for your help, i wouldn't have looked otherwise.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Glad it helped.
Post Reply