0

I have a class:

class Site { public static $url; } 

I assign a value to it: Site::$url = "whatever";

I have another class:

class Something { public function __Construct() { echo Site::$url; // does not seem to have scope? } } 

How do I give scope to the Something class.

2
  • are these classes in the same file? otherwise use include() Commented Aug 19, 2013 at 15:19
  • 2
    If they're in the same file, that should work correctly. I've just copied your code and it seem to be working fine, cannot recreate. Just double check you're doing Site::$url = "whatever"; before calling new Something(); Commented Aug 19, 2013 at 15:24

1 Answer 1

3

It should work as you've written it above:

class Site { public static $url; } class Something { public function __construct() { echo Site::$url; } } Site::$url = 'whatever'; new Something(); // prints 'whatever' 

If that's not working, it's likely because the classes are being declared in two different namespaces.

Sign up to request clarification or add additional context in comments.

1 Comment

Just to add to this, if you are using separate files, you may want to ensure you are using require as opposed to include. This way missing scripts are flagged up sooner. Also having a good autoloader function is a must, just to cover your back ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.