Jump to content

passing X amount of instances of class to another class.


MasterACE14

Recommended Posts

Good Day,

 

I have a class that includes the page requested, but I need to pass the instance of several different classes to this 'loadbackend' class, so the page that the class includes has access to these instances. Either that, or bring the included page out of the scope of the class it's included in.

 

I have things set up like this(relevant code only):

 

index.php:

# Connect to Database
$db = new MySQL('localhost', DB_USER, DB_PASS, DB_NAME);

# Online Users
$online = new Online($db);

# User Session
$session = new Session($db);

# Load Backend
$lb = new LoadBackend($db, PATH_CG.PATH_BACKENDS); /* page is included within this class, either bring that page to the scope of index.php, or pass all the classes above into this class */

 

 

Class LoadBackend {

    private $db;
    private $location;

    public function __construct(Database $db, $location) {
        $this->db = $db;
        $this->location = $location;
        $this->load_backend();
    }

    private function load_backend() {
        global $content;
        
        if(!isset($this->location))
            $location = 'controllers/backend/';
        else
            $location = $this->location;
        if(isset($_GET['page']))
            $_GET['page'] = $_GET['page'];
        else
            $_GET['page'] = 'home';

            $_GET['page'] = htmlentities(trim(str_replace(chr(0), '', $_GET['page']))); // remove Null Bytes
                if(file_exists($location.$_GET['page'].EXT)) {
                    require_once($location.$_GET['page'].EXT);
                } else {
                    require_once($location.'home'.EXT);
                }
        return true;
    }
}

 

Thanks in advance!

 

Kind Regards,

Ace

Link to comment
Share on other sites

You can use an ABSTRACT class which can force every single other class to use the LOADBACKEND's constructor, or however you go about handling the instances.

 

Object Oriented needs to work like a machine, it has to be all connected to each other, so make use of design patterns and paradigm.,

Link to comment
Share on other sites

You can use an ABSTRACT class which can force every single other class to use the LOADBACKEND's constructor, or however you go about handling the instances.

But then the classes can't use their own constructor?

 

If I have this...

 

index.php:

# Connect to Database
$db = new MySQL('localhost', DB_USER, DB_PASS, DB_NAME);

# Online Users
$online = new Online($db);

# User Session
$session = new Session($db);
    
        if(!isset($location))
            $location = 'controllers/backend/';
        else
            $location = $location;
        if(isset($_GET['page']))
            $_GET['page'] = $_GET['page'];
        else
            $_GET['page'] = 'home';

            $_GET['page'] = htmlentities(trim(str_replace(chr(0), '', $_GET['page']))); // remove Null Bytes
                if(file_exists($location.$_GET['page'].EXT)) {
                    require_once($location.$_GET['page'].EXT);
                } else {
                    require_once($location.'home'.EXT);
                }

Taking the method out of the loadbackend class, and just placing that code in index.php, everything works fine(being in the same scope). For the sake of cleaner, more readable and more maintainable code I've made the loadbackend class for this(as this will be used on several sites instead of just the one, I don't want to hard code the class instances being passed to loadbackend)

 

There must be an easier way to do this?

Link to comment
Share on other sites

lol wait wait....

forget this part is in there...

Database $db

 

My issue is with scope correct?

 

so virtually I want to do this...

public function __construct($location, Database $db, $session, $online) {

but with different websites, they will need more or less classes passed to LoadBackend as every website has different requirements.

 

Link to comment
Share on other sites

I see, I thought you were just looking for a more efficient way of doing this, the first code should work no problem.

 

$lb = new LoadBackend($db, PATH_CG.PATH_BACKENDS); /* page is included within this class, either bring that page to the scope of index.php, or pass all the classes above into this class */

 

But yes your correct.

Link to comment
Share on other sites

I'm going to run with this for now until I come up with something better...

# Connect to Database
$db = new MySQL('localhost', DB_USER, DB_PASS, DB_NAME);

# Online Users
$online = new Online($db);

# User Session
$session = new Session($db);
    
        if(!isset($location))
            $location = 'controllers/backend/';
        if(isset($_GET['page']))
            $_GET['page'] = $_GET['page'];
        else
            $_GET['page'] = 'home';

            $_GET['page'] = htmlentities(trim(str_replace(chr(0), '', $_GET['page']))); // remove Null Bytes
                if(file_exists($location.$_GET['page'].EXT)) {
                    require_once($location.$_GET['page'].EXT);
                } else {
                    require_once($location.'home'.EXT);
                }

 

Thanks again for your help! Cheers.

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.