Forum Moderators: coopster

Message Too Old, No Replies

Class Referencing

How do you reference classes properly?

         

Sekka

1:21 pm on May 4, 2007 (gmt 0)

10+ Year Member



Hi,

I may already be doing this the right way but I thought I would ask the community.

I have a system built purely out of classes. One class inparticular is the core of the entire thing and almost every other class uses functions from it.

Now, I currently reference this class using,

$GLOBALS['platform'] -> ...

($platform being the variable the class is stored in)

Now, I am here to ask what is the correct way of referencing this class by all the other classes within my system? Should I pass $platform to all classes that will use it or am I already doing it the correct way by just accessing it within the GLOBALS array?

Thank you,

Steve

dreamcatcher

8:48 pm on May 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From the PHP website:

Often you need classes with similar variables and functions to another existing class. In fact, it is good practice to define a generic class which can be used in all your projects and adapt this class for the needs of each of your specific projects. To facilitate this, classes can be extensions of other classes. The extended or derived class has all variables and functions of the base class (this is called 'inheritance' despite the fact that nobody died) and what you add in the extended definition. It is not possible to subtract from a class, that is, to undefine any existing functions or variables. An extended class is always dependent on a single base class, that is, multiple inheritance is not supported. Classes are extended using the keyword 'extends'.

<?php
class Named_Cart extends Cart {
var $owner;

function set_owner ($name) {
$this->owner = $name;
}
}
?>

dc

Sekka

4:38 pm on May 7, 2007 (gmt 0)

10+ Year Member



I know about extends.

Take the following,

I have a base class which settings are assigned to. These settings effect every class that is run and can be changed by every class too.

Essentially it is a global object, not class, that I need to pass around to everything else.

This make more sense? For this example, I don't think extends cuts it.

borntobeweb

8:50 pm on May 7, 2007 (gmt 0)

10+ Year Member



Hi Sekka, I think what you're looking for is the singleton pattern [google.ca].

So if your singleton class is called Platform, any other classes that need to reference it call:

$platform = Platform::getInstance();

Hope this helps.