Jump to content

[SOLVED] How to make something global?


Imtehbegginer

Recommended Posts

I have a system.php file with a class called wowcms.

In wowcms there is a function called show_error($error);

 

So on my index.php page I include it and do

$wowcms = new wowcms;

 

I also have a template function that includes the header.tpl, index.tpl, and footer.tpl.

 

But when I do a $wowcms->show_error('blahblahblahblah'); nothing comes up.

 

But if I do it on index.php it works..

 

What do I do so that i can use it globally?

Link to comment
https://forums.phpfreaks.com/topic/166137-solved-how-to-make-something-global/
Share on other sites

Is there a problem with instantiating a new wowcms object? Did you consider making the method static? Can you pass around the wowcms object between routines? As a last resort, can the wowcms object be a singleton? (I'm not a fan of singletons, but it's still a little better than a global variable)

 

There are plenty of ways to do it other than using global variables :D

class wowcms {
var $config;

function config() {
	@include('includes/config.php');
	$this->config = $config;
}

function load_template($theme) {
	require('templates/'.$theme.'/header.tpl');
	require('templates/'.$theme.'/index.tpl');
	require('templates/'.$theme.'/footer.tpl');
}

function show_error($text) {
	echo($text);
}

function fatal_error($text) {
	die($text);
}
}

 

There is my system.php.

 

Now on my index I use $wowcms = new wowcms;

then i require my template blah blah.

 

Now on the template I do $wowcms->show_error('whatever');

 

nothing shows up.

 

Can you show me an example of what you mean?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.