Vander Posted October 20, 2010 Share Posted October 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/216363-php-variables-oop-etc-i-dont-know-what-to-call-this-but-i-need-help/ Share on other sites More sharing options...
rwwd Posted October 20, 2010 Share Posted October 20, 2010 Not picking fault at all, but, <? isn't a good practise method of declaring a php document, use <?php instead as this makes your code much more transportable. Quote Link to comment https://forums.phpfreaks.com/topic/216363-php-variables-oop-etc-i-dont-know-what-to-call-this-but-i-need-help/#findComment-1124364 Share on other sites More sharing options...
Vander Posted October 20, 2010 Author Share Posted October 20, 2010 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. But I do plan on improving my coding method so I guess that's on my list. Quote Link to comment https://forums.phpfreaks.com/topic/216363-php-variables-oop-etc-i-dont-know-what-to-call-this-but-i-need-help/#findComment-1124473 Share on other sites More sharing options...
ignace Posted October 20, 2010 Share Posted October 20, 2010 Instead of rolling your own you could take a look at Zend_Config which does what you need and is flexible enough to change to whatever your needs. Quote Link to comment https://forums.phpfreaks.com/topic/216363-php-variables-oop-etc-i-dont-know-what-to-call-this-but-i-need-help/#findComment-1124534 Share on other sites More sharing options...
Vander Posted October 21, 2010 Author Share Posted October 21, 2010 How do I delete this thread? Quote Link to comment https://forums.phpfreaks.com/topic/216363-php-variables-oop-etc-i-dont-know-what-to-call-this-but-i-need-help/#findComment-1124802 Share on other sites More sharing options...
trq Posted October 21, 2010 Share Posted October 21, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/216363-php-variables-oop-etc-i-dont-know-what-to-call-this-but-i-need-help/#findComment-1124804 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.