I'm having trouble loading classes in drupal7. Let me start with a WORKING EXAMPLE that doesn't have problems.
If I create a file called sites/all/modules/johnmodule/test.php
with the following contents
require_once(dirname(__FILE__) . "/dompdf-0.5.1/dompdf_config.inc.php");
$obj = new DOMPDF();
Then I go to the url http://mysite.com/sites/all/modules/johnmodule/test.php
, then I get successful page load which is simply a blank page with nothing in it.
Now I tried to include these two lines of code into a drupal module, but it breaks everything. So what I did was I created a sites/all/modules/johnmodule/johnmodule.info
and a sites/all/modules/johnmodule/johnmodule.module
. For the contents of johnmodule.module
, I have the following:
function johnmodule_billing_block_info()
{
$blocks['johnmodule_billing'] = array('info' => t('Billing'), 'cache' => DRUPAL_CACHE_PER_ROLE);
return $blocks;
}
/**
* Implements hook_menu()
*/
function johnmodule_billing_menu()
{
$items = array();
$items['billing'] = array(
'title' => 'My Billing History',
'page arguments' => array(1),
'page callback' => 'johnmodule_billing_view',
'access callback' => TRUE,
'type' => MENU_CALLBACK
);
return $items;
}
function johnmodule_billing_view()
{
require_once(dirname(__FILE__) . "/dompdf-0.5.1/dompdf_config.inc.php");
$obj = new DOMPDF();
}
Then when I go to http://mysite.com/billing/
, I get a server error. When I looked in the apache error logs, I see:
PHP Fatal error: Class 'DOMPDF' not found in /var/www/dev/mysite.com/public_html/sites/all/modules/johnmodule/johnmodule.module on line ZZZ, referer: http://mysite.com/billing
If I comment out the line $obj = new DOMPDF()
, then the page loads fine.
WHy is Drupal not loading classes?
Additional Info
I tried to modify my johnmodule_billing_view()
by using DOMPDF's __autoload() function to force drupal to load the classes that DOMPDF needs like so:
function johnmodule_billing_view()
{
require_once(dirname(__FILE__) . "/dompdf-0.5.1/dompdf_config.inc.php");
$arrClassFix = array();
if ($handle = opendir(dirname(__FILE__).'/dompdf-0.5.1/include')) {
while (false !== ($entry = readdir($handle)))
if(preg_match('/\\.cls\\.php$/',$entry))
$arrClassFix[] = preg_replace('/\\.cls\\.php$/','',$entry);
closedir($handle);
}
foreach($arrClassFix as $cls){ __autoload($cls); echo $cls;}
$obj = new DOMPDF();
}
This made sure drupal loaded DOMPDF class and all dependent classes, but now when the dompdf object (within itself) needs to call one of it's dependent classes, it can't seem to access the classes I already loaded, and i get the following error in the apache logs:
PHP Fatal error: Class 'Abstract_Renderer' not found in /var/www/dev/mysite.com/public_html/sites/all/modules/johnmodule/dompdf-0.5.1/include/inline_renderer.cls.php on line 48, referer: http://mysite.com/billing
But I double checked the arrClassFix and can confirm that Abstract_Rendered was loaded in my foreachloop!
How do I get drupal these DOMPDF classes?