0

I'd like to apologise for the ambiguous title, it's the best one I could think of to define my problem.

I've got a single class in PHP that I want to be invoked from other scripts and I have a few libraries that I want to be able to call functions from, but I want to be able to call those functions from the other libraries via the single class I already have.

class Core
{
// code
}

I want to essentially do the following, Function->Core->Library Function.

The reasoning behind this is that I don't want to have a bunch of classes that get included when the file is run, causing the user to have to remember a bunch of different class names.

This is what I would essentially hope to achieve (but i'm pretty sure this is incorrect syntax) $Core->Data->Get();

Ryan
  • 957
  • 5
  • 16
  • 34
  • May be you can use object by reference to pass object of the core class as an argument to other libraries/classes and then make use of functions of Core class from there ? something like this: http://stackoverflow.com/questions/9331519/php-object-by-reference-in-php5 – Maximus2012 Jul 20 '13 at 15:23

1 Answers1

1

tadaam. That calls for Dependency Injection ;)

class Core
{
    public $lib1;
    public $lib2;
    public function __construct(){
        $this->lib1 = new Lib1Class();
        $this->lib2 = new Lib2Class();
    }



}
Jarek.D
  • 1,274
  • 1
  • 8
  • 18
  • Just to clarify, the issue isn't including the other libraries within the Core class, it's figuring out how to call the class (e.g. Lib1Class) from outside the core class. – Ryan Jul 20 '13 at 18:30
  • either you want to encapsulate access to some libraries and call them via single class using something like `$core->lib1->get()` or I don't understand the real problem here. I think I don't understand the problem of users having to remember lots of class names. Is that some sort of API? – Jarek.D Jul 20 '13 at 19:13
  • I should of been clearer, I just needed confirmation that using `$Core->Lib1->Get();` would actually work. – Ryan Jul 20 '13 at 19:30