Global variables are generally discouraged for various reasons. One reason is that it increases the chance of a name collision if you combine your code with other modules or libraries. Global variables also introduce very serious problems if you are writing code that is multithreaded or highly concurrent. Some typical such problems are race conditions and deadlock. Multithreading and/or concurrency in PHP are extremely rare, but you can probably avoid these problems by defining constants when you initialize your program, before you start running any major processes:
define('MY_DOMAIN', 'example.co.uk');
You still run the risk of a name collision, though. PHP allows the use of Namespaces to reduce this risk.
Good programming practice usually involves feeding such values into your functions and constructors:
// assume this is some configuration file
$my_config = json_decode('./my-config.json');
// and you can feed it into your OOP code via constructor or whatever
$my_object = new myClass($my_config);
If you still want to define a global variable, you can define $domain at some top level of your code and make it visible within functions by using the global keyword:
// defined at the top level of your code
$domain = 'example.co.uk';
function my_func($foo) {
global $domain;
echo "foo is $foo\n";
echo "domain is $domain\n";
}
A slightly better approach is to explicitly use the PHP superglobal variable, $GLOBALS:
$GLOBALS['domain'] = 'example.co.uk';
function my_func($foo) {
echo "foo is $foo\n";
echo "domain is {$GLOBALS['domain']}\n";
}
You can also define constants attached to a class:
class MyClass
{
const CONSTANT = 'example.co.uk';
function showDomain() {
echo self::DOMAIN . "\n";
}
}
echo MyClass::DOMAIN . "\n";
I'm not really sure from your post what you're trying to accomplish, but hope this helps.