Shoutbox

PHP trouble - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP trouble (/showthread.php?tid=55738)

PHP trouble by brian on 02-11-2006 at 09:33 PM

Never though I'd end up posting here, but i'm done.

I am coding a system right now, so in my main file I have between the lines of

code:
$sqlObj = new sql_system($sql_inf);

Then later on, I include another file, but I need the file to use my $sqlObj, but it is not able to access it for some reason.

Never worked alot over classing systems, but no idea what to do here, maybe declare $sqlObj as public, but it's already outside any if's and else's.

Thanks.
RE: PHP trouble by J-Thread on 02-11-2006 at 11:02 PM

You are sure that it isn't declared within a function? Or do you try to use it in a function?

How about doing a really simple test. Make two files, test1.php and test2.php. In test2.php you do something with $sqlObj, very basic.

In test1.php you make the object like above and do:

code:
require("test2.php");

Still doesn't work? Then you should look to the server setting. Does it work now? Then it's your code. Maybe you did define or use it within a function. Use "global $sqlObj;" then.
RE: PHP trouble by rav0 on 02-12-2006 at 08:10 AM

Somebody posted something similar to this before.

Apparantly, if in file1.php, you define a something, and then include file2.php, whatever you defined in file1.php doesn't work. file2.php gets processed by itself, and then pasted into file1.php. You will need to define something in file2.php for it to work, you can't define it in file1.php and include file2.php.


RE: PHP trouble by WDZ on 02-12-2006 at 08:30 AM

quote:
Originally posted by rav0
Apparantly, if in file1.php, you define a something, and then include file2.php, whatever you defined in file1.php doesn't work. file2.php gets processed by itself, and then pasted into file1.php. You will need to define something in file2.php for it to work, you can't define it in file1.php and include file2.php.
That's nonsense. :p Or, at least, there's more to it than that.

Quick test... file1.php
code:
<?php
class wdz {
    var $lazy = 'very lazy';
}
$wdz = new wdz;
require './file2.php';
?>
file2.php
code:
<?php
echo $wdz->lazy;
?>
Executing file1.php will echo the string as expected.