Jump to content

Scope issues


MasterACE14

Recommended Posts

I'm trying to get a handle on this...

 

here is my index.php file...

# 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);

 

 

here is my LoadBackend class for loading a selected page...

<?php

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;
    }
}

?>

 

now my problem is, because I am using 'require_once' to get the selected page within my LoadBackend class, these class instances can't be used by that required file...

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

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

 

now I could hard code them into my LoadBackend class by passing them as arguments like so...

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

but because I will be using this script on several different websites, not all of them will need to use these objects, and some will use different objects. So I don't want to 'hardcode' any objects that aren't 'required' like my database class.

 

What is the best way to go about either putting these objects into the scope of LoadBackend or... bring the required file in LoadBackend into the scope of index.php, the file in which LoadBackend is called?

 

Any help is appreciated!

 

Thanks a million,

Ace

Link to comment
Share on other sites

maybe it's more achievable with just a loadbackend() function rather than a class?

 

function load_backend($location) {
    global $content;
    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);
            }
}

Link to comment
Share on other sites

my problem is, because I am using 'require_once' to get the selected page within my LoadBackend class, these class instances can't be used by that required file...

 

I'm really not sure what you mean by this. Once a file has been required, it's functions / classes are available anywhere.

Link to comment
Share on other sites

the file that is required uses methods from these objects:

# 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);

 

But I'm getting this notice:

Notice: Undefined variable: session in \controllers\backend\login.php on line 7
Link to comment
Share on other sites

A friend has enlightened me as to what I was trying to do, can be done with the following 'registry' class:

<?php

class Registry{
    
    private $registry = array();
    
    public __set( $var, $val )
    {
        $this->registry[$var] = $val;
    }
    
    public function __get( $var )
    {
        return $registry[$var];
    }
}

?>

 

For those who may be trying to achieve something similar.

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.