Jump to content

Ways to use a class within a different class


Goldeneye

Recommended Posts

I do know how to do this but I am curious about whether or not there is a "preferred" way to do this. I know there are a couple ways to use a class (I'll call Alpha_Class) within another class (I'll class Beta_Class)

 

Let's say we have this simple class (Beta_Class):

class beta {
    function foo(){
    
    }
}

 

If I wanted to use the Alpha Class within the Beta Class, I could any number of things. For example:

class beta {
    function foo(){
        $this->alpha = new alpha;
        //$this->alpha->bar();
    }
}

 

Or you could simply use the $GLOBALS array to store instantiated objects in:

$GLOBALS['alpha'] = new alpha;
class beta {
    function foo(){
        //GLOBALS['alpha']->bar();
    }
}

 

You could even declare Alpha_Class as a static class and thus would not need to be instantiated:

static class alpha {
    static function bar(){}
}

class beta {
    function foo(){
        //alpha::bar();
    }
}

 

Those are the only ways I can think of right now. Are there any other ways to accomplish this? I was wondering which way is the best in terms of readability and maintainability.

Link to comment
Share on other sites

The preferred ways are:

 

composition:

class Beta {
    public function __construct() {
        $this->alpha = new Alpha();
    }
}

 

aggregation:

class Beta {
    public function __construct(Alpha $alpha) {
        $this->alpha = $alpha;
    }
}

 

inheritance:

class Beta extends Alpha {}

 

As you notice $GLOBALS isn't part of them.

Link to comment
Share on other sites

Aggregation is a new one to me. I only knew about composition and inheritance but I also knew using $GLOBALS is a poor programming practice as it can easily lead to overwritten data. I read up about using a Registry (a registry pattern) to store Objects in but that seems counter-productive and abusive to the Registry Pattern.

Link to comment
Share on other sites

The Registry pattern is a global object and thus best avoided. Martin Fowler himself notes it should be a last resort instead of your first weapon of choice.

 

I read up about using a Registry (a registry pattern) to store Objects in but that seems counter-productive and abusive to the Registry Pattern.

 

How is that abusive? A Registry is used to store objects.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.