在另一个PHP类中使用许多不同对象实例的最佳方法是什么?

I have a class called Page, which loads a PHP file for the current page, containing all the HTML (template file). In my template file, I want to use instances of other objects, that is initialized outside the Page class (ex. User, PDO or other classes). My problem is, how I do this the smartest way.

In my page class i have a method called get_page() which loads my template file (containing all the code for my GUI.

public function get_page() {
        // Load theme template
        ...

        $template_file = ABSPATH_THEME . 'tpl.' . $result['template_file'] . '.php';

        if(file_exists($template_file)) {
            $page = require_once($template_file);
            return $page;   
        }
    }

As you see the template file are loaded inside my Page class, and therefore will it not access instances of classes initialized outside my Page class.

I can come up with different solutions:

1) I pass all instances of the different classes to my class Page, when constructing my page. I think this is the right way, but can be very complex if I need 5, 10 or 20 different objects in my design.

2) Find a way to include the template file outside the Page class, but triggered from the get_page() function - have no clue how to do this, and if it is a good solution?

Can you please tell me what is best, and if there are some better ways to do it?

You can just include classes on the top of your php

eg. /CLASSPATH/ClassName.php

And then you can create an entity for that class once and use it everywhere

eg. $entity = new ClassName();

I think your best bet will be to pass them in as arguments, depending on what you need you may want to make them properties.

Edit: This is assuming that by "using instances of other objects" you mean that you need to use objects that have already been instantiated elsewhere.

If by "istances of other classes" you mean other generical objects (you mention PDO and user which are goos examples of this) I would store these in the $_SESSION array (or in $_GLOBAL array, $_SESSION being the best option in most cases).

Then you can access them just by using $_SESSION['PDO']->... and the like

One of the most popular method to handle php application rooting is to implement MVC design patern, or use an MVC Framework. Google 'MVC php' for details, good luck.

Take a look at the functions get_defined_vars and extract. Using these you can export variables from one scope to another.

A silly example could be: A function A transfer locally defined variable to a function B.

function A()
{
    $var1 = "1";
    $var2 = "2";
    // etc

    $data = get_defined_vars();
    B($data);

}

function B($data)
{
    extract($data);
    // somescript.php can use $var1, $var2, etc if B is call from A.
    require("somescript.php");
}

A();

The best practice is to use a ServiceLocator or an InversionOfControll-Container to retrieve class instances without violation of DI-Principle. For your template file you can create view helper objects to have direct access to other objects.