By Whiskey on
Hi all,
I'm trying to use a third party script to enable logins with the Invision Powerboard forum. There's a great project that allows you to do just that, ipbwi (http://ipbwi.pc-intern.com/), but it works by creating a class. My question is how I can create a class and use the instance of it across all the functions in my module. If I happen to make an instance of it twice, things break and besides it would not seem ideal from a performance standpoint to create a different instance for each function.
Comments
Found this link on the
Found this link on the subject: http://www.webthatworks.it/d1/page/globals_drupal_modules
But what is proposed there does not work for me. Also, if I check if the class has been created with
if(empty($class))it will not always work as a new class will be sometimes be created while the then following error indicates the class was created before. But if that is the case, how can I then use it?If the class is included in
If the class is included in a module, it can be accessed globally, be it your own module or an existing one.
Thanks, but that doesn't
Thanks, but that doesn't seem to work for me. For example, I have a module called ipbwi that provides the class and a module called test that uses this class. For testing purposes I have set up this code for the ipbwi module (using hook_boot to make sure the class is created before it is used anywhere else):
and this in the test module:
This will give me an error saying Fatal error: Call to a member function get_info() on a non-object in /home/clanbeware/domains/clanbeware.nl/public_html/Drupal/drupal-6.2/sites/all/modules/test/test.module on line 54 where line 54 is the
$ipb_user_info = $SDK->get_info();line.I have tried using the global and static keywords but to no avail. Even if I load the class in the same module I'm getting into problems, but that was with a more complex module. I am now working with the test module to understand what is happening. Any pointers are greatly appreciated, if you need more details, please let me know!
Isn't there somebody that
Isn't there somebody that can say something on this subject? If I can't get some more insight, I will have to take the class apart and implement the functions separately but I would really hate to do that.
How do you reference $SDK
How do you reference $SDK from the function test_help()? I am not familiar with hook_boot(), but I think that you need to aleast do the following:
You may need something similar in your other function as well, but I don't know for sure.
Tried using the global keyword but without success
I have played with the global keyword and had also tried your suggestion, but I couldn't get it to work. I also tried the possible solutions suggested on http://www.webthatworks.it/d1/page/globals_drupal_modules but those wouldn't work for me either.
Thanks for your response by the way.
I'd do it like this:
I'd do it like this:
this, for example is myclass.class:
then your module file mymodule.module:
Hope this helps
Thanks Meric, but that won't work :(
Thanks Meric, but that won't work (tried just the same to be sure). What happens in your suggestion is that a new instance of the class is created each time the function is used. And that is exactly what this class won't allow. I can't have more than one instance of the class and thus need to always use the same instance. Problem is that I cannot reliably check for and use that instance. I have tried a few options that I refer to above, but none seem to do the job. But I will also immediately admit that I'm no expert with either classes or Drupal, so if I'm missing something or if I got it just plain wrong, please point out my flaws ;)
It looks like PHP scoping
It looks like PHP scoping confusion more than Drupal.
You need to declare $SDK as global in both the initializing function and and funcs that expect to use it. Scope does not inheirit in PHP.
And check that ipbwi_boot is running as expected. I'd probably choose hook_init instead ... but for no special reason. Or, in a simple case just leave those lines in global scope;
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Thank you all for your
Thank you all for your comments. I decided to solve this with a workaround by making sure all functionality that uses the class in question is initiated from hook_init. I'm not sure why the other suggestions wouldn't work in this case (tried them all), but thanks for helping.