Jump to content

PHP, variables, OOP, etc... (I don't know what to call this, but I need help)


Recommended Posts

Hai thar all you smexy things...

 

I has a problem, and I have come to the lovely folk here for help and/or advice...

 

Basically when making a set of scripts I usually have some files dedicated to JUST variables (usually in the form of a associtive array) and now I feel that I don't like including EVERY variable file into my scripts because I use less than half of the files at any one time in most cases and I feel it's inefficient, so I've been trying to look into a way to call variables into a class as I need them...

Basically what I've tried to do is make a class that I can call by doing something like "$_VAR->var_name['key'];" but when I use OOP5s __get method it only takes the "var_name" and leaves out the rest from what I've done, so I then tried to do something like "$_VAR->var_name('key');" and used the __call, but that didn't work out too well either, it would only return a NULL value.

 

Here is the class I've got just now.

 

<?
class Vars{
    private $var_path;
    function __construct($path=NULL){
        if(isset($path)):
            $this->var_path = $path;
        else:
            $this->var_path = DOC_ROOT.'/vars';
        endif;
    }
    
    function __call($name,$arguments){
        if(isset($this->$name)):
            // Do nothing.
        elseif(is_dir($this->var_path.'/'.$name)):
            include $this->var_path.'/'.$name.'/'.$arguments[0].'.php';
            if(isset($this->$name)):
                $this->{$name}[$arguments[0]] = $var;
            else:
                $this->$name = array();
                $this->{$name}[$arguments[0]] = $var;
            endif;
        elseif(is_file($this->var_path.'/'.$name.'.php')):
            include $this->var_path.'/'.$name.'.php';
            $this->$name = $var;
        else:
            //Return NULL
            return NULL;
        endif;
        unset($var);
        $return = $this->$name;
        for($x=0;$x<$y=count($arguments);$x++):
            $return = $return[$arguments[$y]];
        endfor;
        return $return;
    }
}
?>

 

And this is how I would want to call the "title" feild from the layout.php file.

 

<?
$_ = new Vars('C:/xampp/htdocs/inc/vars');
echo $_->layout('title');
?>

 

I hope you can understand that, if not, tell me what you don't understand and I'll explain.

 

But what I'm here for is either help to do what I'm wanting to do right now, or some advice to point me in the right direction.

 

Thanks in advance,

Alan.

Aye, you do make a good point, but since I only use my own computers as servers and configure my domains and routers to what I need, I just set short tags to on all the time. :P But I do plan on improving my coding method so I guess that's on my list.

I fail to see how this could help me, any chance you could elaborate a little bit on how I would use Zend_Config to help me?

 

I think ignace covered it pretty well with...

 

does what you need and is flexible enough to change to whatever your needs

 

Do you not understand something in particular?

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.