1

Generally when creating a PHP class I would do something as such

class Foo
{
    public function Foo(){}
    public function RandomFunction(){};
}
global $foo;
$foo = new Foo();
$foo->RandomFunction();

I have noticed that on the web people are frowning upon the global vars for classes but without it I would not be able to access the class inside other functions.

Now an alternative to this would be using static classes as such:

class Foo
{
    static public function RandomFunction(){}
}
Foo::RandomFunction();

My Question is this, is this the best alternative to global vars for classes? Or is there a better way to go about it all together?

3
  • I'm not really sure what you are aiming at. Why does $foo need to be global in your example? If you want to access that instance in other functions, you should pass it as parameter. I have the feeling you are using classes a container for (arbitrary) functions... Commented Jun 2, 2011 at 14:18
  • 1
    Please start using __construct if you're using PHP5. Commented Jun 2, 2011 at 14:26
  • Well for example wordpress uses the global $foo within it's functions so the $foo class can be accessed from other functions or classes. Passing it as a parameter sounds like a bad idea. Commented Jun 2, 2011 at 14:27

2 Answers 2

4

In good OO practice, if a class needs to access an instance of another class, you pass object in as a parameter and then access it's properties and methods from there.

If you have a well designed object model the code should be relatively clean and logical, however if you aren't planning on creating a strict OO application, you probably shouldn't worry about your classes being in global scope or not.

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

3 Comments

Would you really pass it as a parameter? That doesn't seem like it would be very efficient to do that. But then again I am newer to this.
Yes, absolutely. If a class needs an instance of another class it makes just as much send to pass an object in as a parameter than any other data type. If you had a Person() class, you could pass it ("food","icecream","200 calories") or a food object that has all this information and the related methods. Reading up on some design patterns should clear this up.
Try not to read too deeply into the metaphors. Just think of a class as a normal data type, like an int or a string with some extra bells and whistles you can use. Good question though, and good luck in your future studies. +1
2

Think of your object like a bicycle. You own your bicycle, and can loan it to whomever you wish. They can use it and return it to you, and you will be able to keep track of who did what to your bike, and when.

If your bike is generally available to anyone in the world, however, your job of monitoring and controlling access to it will become exponentially more difficult. You will be hard-pressed to keep track of who is using your bike, when they are using it, and how they are (ab)using it.

Passing your objects (rather than declaring them globally) allows you to maintain stricter controls on your object and the data it contains.

3 Comments

Passing as a reference as in & $classname?
Recent versions of PHP automatically pass object by reference to functions, so you do not need to use '&'.
So in essence I can just do $foo->RandomFunction($object, $otherparam). Makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.