class and globals in functions (maybe PHP)
I've to access an external DB (MS SQL so it is too complicated to rewrite the DB layer... bla bla) So I plugged in my class to be seen inside hooks... //catalog_base.inc $param="salve"; class Catalog_DB { function __construct($DBParam) { //... } public function test() { print("<!-- ciao ciao -->"); } } $DBGorilla=new Catalog_DB($param); //catalog.module $path = drupal_get_path('module', 'catalog'); require_once($path . '/catalog_base.inc'); //... hooks etc... //test1 $DBGorilla->test(); function catalog_asearch() { global $DBGorilla; //test2 $DBGorilla->test(); $query=$DBGorilla->test(); return ""; } test1 succeed test2 fail with Call to a member function test() on a non-object in ... using $GLOBALS makes no difference... but drupal code is plenty of global $user; in functions what's wrong? thx -- Ivan Sergio Borgonovo http://www.webthatworks.it
I simplified as much as I could and I came out with: <? class tonno { protected $pippo=null; function __construct($pippo) { $this->pippo=$pippo; } public function test() { print($this->pippo); } } $zoppas=new tonno("morto"); function cane() { global $zoppas; $zoppas->test(); } cane(); ?> That works perfectly as a separated app in drupal environment (that means same server, same php.ini, same dir where drupal reside). then I moved the same code inside my catalog.module but I put the global $zoppas; $zoppas->test(); in a hook and again: Fatal error: Call to a member function test() on a non-object in /srv/twww.mebsbooks.com/d1/sites/all/modules/catalog/catalog.module on line 63 what's different in the module execution context? why global $user; works? -- Ivan Sergio Borgonovo http://www.webthatworks.it
participants (1)
-
Ivan Sergio Borgonovo