0

I'm having some trouble creating a "super object" (similar to that of CodeIgniter) for a framework project I'm working on.

I want to make a super object that all of my other classes will run through. I did that, but it seems I am unable to use my super object's object in each of my classes without using the global keyword in every function.

I have something like this:

class a
{
    function aa()
    {
        return 'class a';
    }
}

class b
{
    function bb()
    {
        $blah->a->aa();
    }
}

class superobj
{
    function loadClass($class)
    {
        $this->$class = new $class;
    }
}

class blah extends superobj
{
    function __construct()
    {
        $this->loadClass('a');
        $this->loadClass('b');
    }
}

$blah = new $blah;

So if I run this, I get an error because I can't access the aa method. If I put global $blah into the method bb, then it works (or at least in my real project it does, I dunno about this 5 second mockup hehe).

So is there any way to either make the $blah object global so that I don't need to reference it for every method, OR is there a better way to achieve what I am trying to do?

I'm not very good at OOP...

Scott
  • 11
  • 1

1 Answers1

3
function bb() {
    $blah->a->aa();
}

Indeed, as with any function, this function has no variables in its scope. $blah was neither passed to the function nor is it defined in the function, so it doesn't exist.

If your objects depend on an instance of blah to work, you should pass it to the constructor.

class b {

    var $blah = null;

    function __construct($blah) {
        $this->blah = $blah;
    }

    function bb() {
        $this->blah->foo();
    }
}

$b = new b($instanceOfBlah);

Alternatively, use a static class that holds references to global objects and returns them on request (Registry pattern, like $blah = Registry::get('blah')).

Don't use global. Just don't.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Is there a way I can do this to blah's constructor, so that every class that extends blah would automatically be able to use its methods? – Scott Nov 19 '10 at 05:43
  • @Scott If a class *extends* blah, of course it'll be able to use blah's methods just like its own. – deceze Nov 19 '10 at 05:45
  • I will try to explain this, bare with me. If I load "class b" through "superobj", just like I am doing in my example above, I cannot use "class a"'s methods. If I load "class b" outside of superobj, then it DOES work. However I need it to be able to load through superobj, so I only ever have to use one object for all of my libraries. EDIT: Note that in my above example I am not extending blah, however in my real project I am, and calling $this->a->aa, for example, does not work. – Scott Nov 19 '10 at 05:51
  • @Scott Sorry, I'm not sure what you're aiming at here. You may be misunderstanding something about how OO works. You're also talking about two separate things: *extending objects* and *accessing methods of other objects* and how these objects should be passed around. Maybe you could provide a more concrete example about what you want to achieve, with more useful names than "a" and "blah"? – deceze Nov 19 '10 at 06:35
  • Well basically, I am trying to get around the lack of multiple inheritance in PHP. I would like to create a "master" class that all other classes run through, so that I have one simple object that can access all methods. I essentially want what the CodeIgniter framework does. I suppose maybe I will have to spend the time to dig through the CodeIgniter core and see what is going on. – Scott Nov 19 '10 at 07:37
  • @Scott Okay... The best way around multiple inheritance (actually, the best way, period, even if multiple inheritance existed) is composition. You're doing that with the `blah` class; a `blah` object is composed of a bunch of other objects, so `blah` has access to (methods of) `a` and `b`. `a` and `b` are *not* composed objects though, they're stand alone objects, neither inheriting nor otherwise acquiring any additional methods. `a` and `b` will either have to do the same thing `blah` does (extension and composition) or use one of the methods I described above. – deceze Nov 19 '10 at 07:44