I know the difference between include
and require
. And I use require
in several places in my project.
But here is the thing: I want to include files ONCE and be able to call it's functions from other include files.
In my header.php file, I have the following code:
<?php
require_once('include/dal.php');
require_once('include/bll.php');
require_once('include/jquery_bll.php');
?>
Now, in my javascript, I'm calling the jquery_bll.php using jQuery.post, and in jquery_bll.php file I have the following code:
$myDB = new DAL();
My problem is that I get the error message "Class 'DAL' not found in (..)\jquery_bll.php".
I recon I get this message because the file is called from a javascript and therefore it outside of the "scope" or something...
So, is there anyway of including the needed files in header.php and not having to worry about including the same files again?
You need to also put the
require_once 'include/dal.php';
require_once 'include/bll.php';
on top of your jquery_bll.php
, or include your complete header file there.
When your JavaScript calls back to jquery_bll.php, this is a separate HTTP request, which will cause PHP to only evaluate that file. As that file does not seem to include your header.php, the require_once
statements that make sure the DAL class is loaded are not executed.